A
Active8
I can't see the problem here in these frags. Try/catches don't help
- nothing thrown. I've run out of ideas. I'm just generating a
cosine wave of 400 values, writing the values to disk, reading them
back and plotting them.
Any help fixing this will be appreciated.
int DSPFile::write_double(double samp_val)
{
fwrite(&samp_val, sizeof(double), 1, m_channel);
return 0;
}
int DSPFile::read_double(double* samp_val)
{
fread(samp_val, sizeof(double), 1, m_channel);
return 0;
}
int i;
double f_s = 2000.0; // samp rate Hz
double d_t; // time increment
double f_1 = 200.0; // 1st frequency component
double f_2 = 60.0; // 2nd frequency component
double A_1 = 10.0; // peak amplitude
double A_2 = 10.0; // peak amplitude
d_t = 1/f_s;
#define PI (3.141592653589793)
#define PI2 (6.283185307179586)
double x_n;
for(i=0;i<400;i++)
{
x_n = A_1 * sin(PI2 * f_1 * d_t * i);
//x_n += A_2 * cos(PI2 * f_2 * d_t * i);
//x_n /= 2;
out_file.write_double(x_n);
}
out_file.Close();
double y[400];
DSPFile in_file;
in_file.OpenRead("out.dat");
for(i=0;i<400;i++)
{
in_file.read_double(y+i);
}
double ymax, ymin, ydiv, ymedian, ypp, yscale;
ymax = y[0];
ymin = y[0];
for(i=1;i<400;i++)
{
if(y > ymax) ymax = y;
if(y < ymin) ymin = y;
}
ydiv = (ymax - ymin)/10; // amplitude per vert plot division
ymedian = ymin + (ymax - ymin)/2;
ypp = ymax - ymin;
yscale = 400/ypp;
//------- just checking ------------//
wxString szwx_tmp;
szwx_tmp.Printf("%4.3e, %4.3e, %4.3e, %4.3e", ydiv, ymedian,
ypp,yscale);
wxMessageBox(szwx_tmp, wxT("Limits and such"), wxOK | wxCENTRE);
//--------------------------------//
in_file.Close();
It works for f_s = 2000, but not 20000.
It works for f_1 = 200 or more, but not less.
Different amplitudes are ok at f_1 >= 200;
It doesn't work when I add the 2nd freq component. Sometimes it goes
to zero, sometimes it looks like it goes off the plot.
I can't see overflow being the prob since higher freqs (larger arg
for cos) work and lower ones (f_1 < 200 || f_s > 2000) don't work.
It looks like it goes ok up to 180 samples* and then goes to zero
for the rest. Sometimes it looks like it goes off the plot and my
scale factor only worked for f_1 = 200, f_s=2000.
The max/min median, etc., numbers are astronomical, infinitessimal,
or zero for failed cases.
* i tried 4*atan(1) for 2*PI ( PI2 ) and now I get what looks like
350 data points (definitely not 360, though) rather than 180 before
it goes to zero. Weird.
Thanks for the help.
- nothing thrown. I've run out of ideas. I'm just generating a
cosine wave of 400 values, writing the values to disk, reading them
back and plotting them.
Any help fixing this will be appreciated.
int DSPFile::write_double(double samp_val)
{
fwrite(&samp_val, sizeof(double), 1, m_channel);
return 0;
}
int DSPFile::read_double(double* samp_val)
{
fread(samp_val, sizeof(double), 1, m_channel);
return 0;
}
int i;
double f_s = 2000.0; // samp rate Hz
double d_t; // time increment
double f_1 = 200.0; // 1st frequency component
double f_2 = 60.0; // 2nd frequency component
double A_1 = 10.0; // peak amplitude
double A_2 = 10.0; // peak amplitude
d_t = 1/f_s;
#define PI (3.141592653589793)
#define PI2 (6.283185307179586)
double x_n;
for(i=0;i<400;i++)
{
x_n = A_1 * sin(PI2 * f_1 * d_t * i);
//x_n += A_2 * cos(PI2 * f_2 * d_t * i);
//x_n /= 2;
out_file.write_double(x_n);
}
out_file.Close();
double y[400];
DSPFile in_file;
in_file.OpenRead("out.dat");
for(i=0;i<400;i++)
{
in_file.read_double(y+i);
}
double ymax, ymin, ydiv, ymedian, ypp, yscale;
ymax = y[0];
ymin = y[0];
for(i=1;i<400;i++)
{
if(y > ymax) ymax = y;
if(y < ymin) ymin = y;
}
ydiv = (ymax - ymin)/10; // amplitude per vert plot division
ymedian = ymin + (ymax - ymin)/2;
ypp = ymax - ymin;
yscale = 400/ypp;
//------- just checking ------------//
wxString szwx_tmp;
szwx_tmp.Printf("%4.3e, %4.3e, %4.3e, %4.3e", ydiv, ymedian,
ypp,yscale);
wxMessageBox(szwx_tmp, wxT("Limits and such"), wxOK | wxCENTRE);
//--------------------------------//
in_file.Close();
It works for f_s = 2000, but not 20000.
It works for f_1 = 200 or more, but not less.
Different amplitudes are ok at f_1 >= 200;
It doesn't work when I add the 2nd freq component. Sometimes it goes
to zero, sometimes it looks like it goes off the plot.
I can't see overflow being the prob since higher freqs (larger arg
for cos) work and lower ones (f_1 < 200 || f_s > 2000) don't work.
It looks like it goes ok up to 180 samples* and then goes to zero
for the rest. Sometimes it looks like it goes off the plot and my
scale factor only worked for f_1 = 200, f_s=2000.
The max/min median, etc., numbers are astronomical, infinitessimal,
or zero for failed cases.
* i tried 4*atan(1) for 2*PI ( PI2 ) and now I get what looks like
350 data points (definitely not 360, though) rather than 180 before
it goes to zero. Weird.
Thanks for the help.