1

データ配列(0>359deg) = 位相 + ((正弦値 * ゲイン +- ゲイン ノイズ値に正規化)+バイアス)

y=(G×sin(θ+φ)+-N)+バイアス

これは私がこれまでに持っているものです。Excelでグラフを設定すると、何らかの理由でノイズが正しく表示されません。

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <ctime> // convert time value to string
#include <cstdlib>  // standard general utilities library "# generator"
#include <fstream>  // writing data to disk

using namespace std;

#pragma warning(disable:4996)

int _tmain(int argc, _TCHAR* argv[])

{

srand((unsigned)time(0));

// declarations
FILE *fptr;
char fileName[50] = "";
double freq;
double gain;
double phase;
double bias;
double rand_noise = ((double)rand()/ ((RAND_MAX)) - 0.5); 
char save;
double noise;

const long double PI = acos((long double) -1);


// user inputs for sine wave values

cout<<"Enter Frequency: [1-10Mhz] ";
cin>>freq;
cout<<"Enter Gain of Sine Wave: [1-10] ";
cin>>gain;
cout<<"Enter Phase Angle: [0-180] ";
cin>>phase;
cout<<"Enter Bias (offset) [+/- 10] ";
cin>>bias;
cout<<"Enter % Noise level introduced to sine (percent) [0 - 100%] ";
cin>>noise;
cout<<"Do you want to save the data [y/n]: ";
cin>>save;


if (save == 'y'){
cout<<"Enter a file name you wish to use: ";
    cin>> fileName;
}


double timeint = (double)1/freq/360;
long double interval = (2*PI)/360;
long double sinevalue;
if (save == 'y') {
sprintf(fileName, "%s.csv", fileName);
fptr = fopen(fileName, "w");
fprintf(fptr, "%s,", "Time(1/f)");
fprintf(fptr, "%s\n", "Sine Pattern");
}

for(int i=0; i<=360; i++){
sinevalue = (gain*sin((interval*i)+phase)+(rand_noise*(noise/100)*gain))+bias;
if (save == 'y') {
    fprintf(fptr, "%f,", timeint*i);
    fprintf(fptr, "%f\n", sinevalue);

}

}

return 0;

}
4

1 に答える 1

1

関数は 0 からRAND_MAXrand()までの乱数を生成します

-0.5 から 0.5 の間になるようにするには、次のようにする必要があります: rand() 0 から 1 の間

#include <cstdlib>

//...
for(int i=0; i<=360; i++){

  double rand_noise = ((double)rand()/ ((RAND_MAX)) - 0.5; //divide rand() by RAND_MAX to get between 0 and 1, then subtract 0.5 to be between -0.5 and 0.5.
  sinevalue = (gain*sin((interval*i)+phase)+(rand_noise*(noise/100)*gain))+bias;

  if (save == 'y') {
    fprintf(fptr, "%f,", timeint*i);
    fprintf(fptr, "%f\n", sinevalue);

  }

}
于 2013-04-10T02:23:27.883 に答える