By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

math cos, rounding, or other problem?

P: n/a
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[i] > ymax) ymax = y[i];
if(y[i] < ymin) ymin = y[i];
}

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.
--
Best Regards,
Mike
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Active8" <re*********@ndbbm.net> wrote in message
news:1h****************@ID-222894.news.individual.net...
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. .... fwrite(&samp_val, sizeof(double), 1, m_channel); .... fread(samp_val, sizeof(double), 1, m_channel); .... in_file.OpenRead("out.dat");


It would probably help to open the file in binary
mode for both write and read operations.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #2

P: n/a
On Tue, 22 Feb 2005 15:00:35 -0500, Active8 wrote:

The prob is somewhere in the disk write/read.

I plotted the output vals directly and it worked so I changed the
write function to take a data pointer, but it didn't help:

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 x[400];
out_file.write_double(x+i); // in a for loop indexed by i

I don't get it.

<snip>

Thanks for the help.
--
Best Regards,
Mike
Jul 23 '05 #3

P: n/a
"Active8" <re*********@ndbbm.net> wrote in message
news:1p****************@ID-222894.news.individual.net...
On Tue, 22 Feb 2005 15:00:35 -0500, Active8 wrote:

The prob is somewhere in the disk write/read.

I plotted the output vals directly and it worked so I changed the
write function to take a data pointer, but it didn't help:

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 x[400];
out_file.write_double(x+i); // in a for loop indexed by i

I don't get it.

Have you tried opening the file in binary mode for both
the write sequence and the read sequence? If not, why
not? If so, your file system or I/O library is broken. The
code you posted earlier should have retrieved the same
values as were written except for the binary mode problem.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #4

P: n/a
On Tue, 22 Feb 2005 12:42:06 -0800, Larry Brasfield wrote:
"Active8" <re*********@ndbbm.net> wrote in message
news:1p****************@ID-222894.news.individual.net...
On Tue, 22 Feb 2005 15:00:35 -0500, Active8 wrote:

The prob is somewhere in the disk write/read.

I plotted the output vals directly and it worked so I changed the
write function to take a data pointer, but it didn't help:

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 x[400];
out_file.write_double(x+i); // in a for loop indexed by i

I don't get it.


Have you tried opening the file in binary mode for both
the write sequence and the read sequence?


That was a smart guess, Larry. I used part of some old code from a
book cd that had the open flag "wt" which doesn't even show up in
any refs I can dig up at this time. I don't know how that author
made it work with that flag. It was old DOS code in C and I got
tired of fixing the stuff that wouldn't fly and just started from
scratch with "wt" firmly stuck in my head. It rings a bell, but I
can't find a definition for it.

Thanks.
--
Best Regards,
Mike
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.