Please download the reference first. Matrix matrix class (matlab2c.h file).
This time, we will give you c++ to implement the matlab function on the "signal operation" related functions, including
matlab functions such as awgn, normrnd, fft, sawtooth, sinc, signcdf, signpdf, etc.
Refer to the downloaded matlab2c.h file in your project.
Declare the functions you need to call in your cpp file.
#include "Matlab2c.h" //Add file reference
using namespace Matlab2c; //Add spatial references
//Adding Gauss white noise to the signal in the unit of signal-to-noise ratio (SNR) of dB
Matrix awgn(Matrix& sign,double SNR);
//Generating Orthodox Distribution Matrix
Matrix normrnd(int rowsnum ,int columnsnum ,double aver ,double sigma);
//Complex sequence fft suggests that real sequence can be transformed into complex structure first and then fft
CMatrix fft(CMatrix& a);
//Generation of sawtooth or triangular waves with a positive or negative amplitude of 1//a is a row vector. Generation period is 2pi, and width is the proportion of ascending phase.
Matrix sawtooth(Matrix& a,double width);
//Generating sinc signal input: sinc signal time domain value point a output: sinc signal value at value point
Matrix sinc(Matrix& a);
//Accumulated Probability Density of Signal data at Interval x Point
Matrix signcdf(Matrix& data ,Matrix& x);
//data and x are vectors of probability density functions obtained from signal information
//Hint: Probability density calculation may be different from formula calculation. Here is the integral value at interval point.
Matrix signpdf(Matrix& data ,Matrix& x);
Write the concrete implementation of matrix generating function in your cpp file.
#include "Matlab2c.h"
using namespace Matlab2c;
// Adding Gauss White Noise to Signal
//Input: Real signal, SNR
//Output: Output signal with added column SNR
// Tip: Signal-to-noise ratio in dB
Matrix awgn(Matrix& sign,double SNR)
{
int i;
double sigPower=0;
double noisePower=0;
Matrix p(sign.row,sign.column);
Matrix noise;
srand((unsigned)time(NULL));
for (i=0;i<sign.row*sign.column;i++)
{
sigPower+=pow(sign.data[i],2);
}
sigPower = sigPower/(sign.row*sign.column);
noisePower = sigPower/(pow(10,SNR/10));
noise = normrnd(sign.row,sign.column,0,noisePower);
for (i=0;i<p.row*p.column;i++)
{
p.data[i]=noise.data[i]+sign.data[i];
}
return p;
}
//Generate orthodox distribution matrix
Matrix normrnd(int row,int column,double aver ,double sigma)
{
double x,dScope,y,fengzhi;
int i,j;
Matrix p(row,column);
srand((unsigned)time(0));
for (i=0;i<p.row;i++)
{
for (j=0;j<p.column;j++)
{
do
{
x=((double)rand()/RAND_MAX)*6*sigma+aver-3*sigma; //Mean positive and negative3sigma The value of
y = 1.0/(std::sqrt(2*PI)*sigma)*std::exp(-1*(x-aver)*(x-aver)/(2*sigma*sigma));
fengzhi = 1.0/(std::sqrt(2*PI)*sigma);
dScope = ((double)rand()/RAND_MAX)*fengzhi;
}while( dScope > y);
p(i,j)=x;
}
}
return p;
}
/ / complex sequence fft
//Input: Complex sequence a
//Output: fft of sequence a
// Hint: Real sequences can be transformed into complex structures before fft
CMatrix fft(CMatrix& a)
{
int i,k;
CMatrix p;
int len =a.row*a.column;
double x1,y1;
p.row=a.row;
p.column=a.column;
p.data = new Complex[p.row*p.column];
for(k=0;k<len;k++)
{
x1=0;y1=0;
for(i=0;i<len;i++)
{
x1+=a.data[i].x*std::cos(2*PI*k*i/len)+a.data[i].y*std::sin(2*PI*k*i/len);
y1+=a.data[i].y*std::cos(2*PI*k*i/len)-std::sin(2*PI*k*i/len)*a.data[i].x;
}
p.data[k].x=x1;
p.data[k].y=y1;
}
return p;
}
// Produce sawtooth or triangular waves with a positive or negative amplitude of 1
//Input: Sawtooth wave signal time domain value point a, ascending phase proportional width
//Output: The value of the sawtooth signal at the point of selection
// hint: the amplitude is positive or negative 1, the period is 2 pi, and the width is the proportion of ascending segment
Matrix sawtooth(Matrix& a,double width)//A is a line vector, producing periodic behavior.2pi,width Proportion of ascending segment
{
int i,j;
Matrix p(a.row,a.column);
int n;
double data;
for (i=0;i<a.row;i++)
for (j=0;j<a.column;j++)
{
n=std::floor(a(i,j)/(2*PI)); //Rounding down
data = a(i,j)-n*2*PI;
if (data<=(2*PI*width) && data>0)
p(i,j) = 1.0/(PI*width)*data-1;
else if (data>(2*PI*width) && data<=(2*PI))
p(i,j) = -1.0/(PI*(1-width))*data+(1+width)/(1-width);
}
return p;
}
/ / generate sinc signal
//Input: sinc signal time domain value point a
//Output: The value of sinc signal at the point of value
Matrix sinc(Matrix& a)
{
int i,j;
Matrix p(a.row,a.column);
for (i=0;i<a.row;i++)
for (j=0;j<a.column;j++)
if(a(i,j)==0)
p(i,j)=0;
else
p(i,j) = std::sin(PI*(a(i,j)))/(PI*(a(i,j)));
return p;
}
// Accumulated probability density of signal data at interval x
//Input: Signal data, interval points for calculating cumulative probability
//Output: cumulative probability of signals at interval points
//Tip: Call more internal custom functions
Matrix signcdf(Matrix& data ,Matrix& x)
{
int i;
Matrix p,temp1,temp2;
temp1 = Matlab2c::x_vector(-INT_MAX,x); //Add a negative infinity
temp2 = Matlab2c::histc(data,temp1);
temp2.row--; //Remove the last data, because the last must be 1
for (i=0;i<temp2.row;i++)
temp2.data[i] = temp2.data[i]/(data.row*data.column); //data normalization
p = Matlab2c::cumsum(temp2);
free(temp1.data);free(temp2.data);
return p;
}
/ / Probability Density Function of Data Acquisition Based on Signal Information
//Input: Interval Points for Calculating Probability of Signal data
//Output: Probability Density of Signal Data at Interval Points
// Tip: Probability density calculation may not be the same as formula calculation. Here is the integral value at the interval point.
Matrix signpdf(Matrix& data ,Matrix& x) // data and x Vector
{
int i;
Matrix p;
p = Matlab2c::histc(data,x);
p.row--; //Remove the last data because the interval number is one less than the number of data points.
for (i=0;i<p.row;i++)
p.data[i] = p.data[i]/(data.row*data.column); //data normalization
return p;
}