473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

math cos, rounding, or other problem?

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
4 1562
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
1
by: cody | last post by:
the documentation states that Math.Round supports banker's rounding but it also states that if the last number is 5 it will rounded up if the whole number is even, otherwise rounded down which is...
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
12
by: Test User | last post by:
Hi all, I have learnt that if I want to round 0.5 to an integer the result should be 1, This is also the case if I do it in SQL server 2000, but if I do it in VB.NET the result will be 0. ...
6
by: Mitchell Vincent | last post by:
Just making sure I'm not missing the boat here, but are there any special routines for doing currency math (fixed precision stuff) in .NET? The wonderful problems of doing math on decimals tend...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
15
by: Chris | last post by:
>>from math import * 0.0 1.2246063538223773e-016 -2.4492127076447545e-016 1.0 -1.0 1.0 The cosine function works fine, but I'm getting weird answers for sine. Is this a bug? Am I doing...
3
by: Altman | last post by:
OK I was having rounding problems before and I didn't realize that their was a third parameter in the round function that would tell it if it a 5 to round up. I thought adding this would fix the...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.