C++ 正弦模式生成数据
数据阵列(0>359度)=相位+((正弦值*增益+-归一化为增益噪声值)+偏置) y=(G x sin(θ+ν)+-N)+偏压 这就是我目前所拥有的。在excel中设置图形时,由于某些原因,噪波无法正确显示C++ 正弦模式生成数据,c++,visual-c++,visual-studio-2012,C++,Visual C++,Visual Studio 2012,数据阵列(0>359度)=相位+((正弦值*增益+-归一化为增益噪声值)+偏置) y=(G x sin(θ+ν)+-N)+偏压 这就是我目前所拥有的。在excel中设置图形时,由于某些原因,噪波无法正确显示 #include "stdafx.h" #include <iostream> #include <math.h> #include <ctime> // convert time value to string #include <cstdlib&
#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;
}
#包括“stdafx.h”
#包括
#包括
#包含//将时间值转换为字符串
#包括//标准通用工具库“#生成器”
#包括//将数据写入磁盘
使用名称空间std;
#杂注警告(禁用:4996)
int _tmain(int argc,_TCHAR*argv[]
{
srand((未签名)时间(0));
//声明
文件*fptr;
字符文件名[50]=“”;
双频;
双增益;
双相;
双重偏见;
双随机噪声=((双)随机噪声()/((随机噪声最大值))-0.5);
保存字符;
双重噪声;
const long double PI=acos((long double)-1);
//正弦波值的用户输入
coutfreq;
库特根;
coutphase;
库特比亚斯;
栗色;
coutsave;
如果(保存=='y'){
cout文件名;
}
双时点=(双)1/freq/360;
长双间隔=(2*PI)/360;
长双正弦值;
如果(保存=='y'){
sprintf(文件名为“%s.csv”,文件名);
fptr=fopen(文件名,“w”);
fprintf(fptr,“%s,”,“时间(1/f)”;
fprintf(fptr,“%s\n”,“正弦模式”);
}
for(int i=0;i您的rand()
函数生成一个介于0和之间的随机数
要使其介于-0.5和0.5之间,需要执行以下操作:
#包括
//...
for(int i=0;i您的rand()
函数生成一个介于0和之间的随机数
要使其介于-0.5和0.5之间,需要执行以下操作:
#包括
//...
对于(int i=0;i您所拥有的看起来还可以…您能否更具体地说明噪声如何无法正确显示?另外,刚刚注意到您的if语句中有两个是赋值:save='y'
-您想要save==''y'
您没有使用rand()功能正确…查看我的答案您所拥有的看起来正常…您能否更具体地说明噪声如何无法正确显示?另外,刚刚注意到您的两个if语句是赋值:save='y'
-您想要save=='y'
您没有使用rand()功能正确…请参阅我的回答谢谢你的评论maditya谢谢你的评论maditya
#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);
}
}