This program doesn't initialize X1 prior to its use. Does Qbasic automatically initialize new variables to 0? It also got re-formatted, I'm assuming the program should look like this: And I've added X1 = 0 etc... DIM array(100) DIM ToneFreq AS INTEGER DIM SampleRate AS INTEGER DIM X0 AS DOUBLE DIM X1 AS DOUBLE DIM X2 AS DOUBLE ToneFreq = 440 SampleRate = 10000 PI = 3.1415 A = 5 k = 2 * COS(2 * PI * ToneFreq / SampleRate) X1 = 0 X2 = -A * SIN(2 * PI * ToneFreq / SampleRate) OPEN "LIST.TMP" FOR OUTPUT AS #1 FOR i = 1 TO 100 X0 = k * X1 - X2 X2 = X1 X1 = X0 array(i) = X0 PRINT #1, array(i) NEXT i CLOSE #1 Since sin(2 * X) = 2 * sin(X) * cos(X) if X = 2 * PI * ToneFreq / SampleRate And we make A = 1 then the first X0 = k * X1 - X2 Makes X0 and therefore X1 = SIN(X) So the next time around (since X2 = X1 = 0) the second X0 = k * X1 - X2 is really X0 = 2 * SIN(X) * COS(X) X0 = SIN(2*X) X0 = SIN(4 * PI * ToneFreq / SampleRate) So it would seem to me that it was multiplying the angle with each step. If you added a line like: PRINT X0 - SIN( I * (2 * PI * ToneFreq / SampleRate) ) That doesn't seem right to me. You could see the error in the approximation vs what it is actually trying to calculate. Or did I miss something there? --- James Newton: PICList webmaster/Admin mailto:jamesnewton@piclist.com 1-619-652-0593 phone http://www.piclist.com/member/JMN-EFP-786 PIC/PICList FAQ: http://www.piclist.com > -----Original Message----- > From: piclist-bounces@mit.edu > [mailto:piclist-bounces@mit.edu] On Behalf Of Dennis Crawley > Sent: 2004 Sep 28, Tue 05:31 > To: Microcontroller discussion list - Public. > Subject: Re: [PIC] Another method of generating and detecting > sine waves > > > Andy, > I've try this QBasic program to make a file to be analized by > a spreadsheet > > "A" seems to be the amplitud. > The wave form is irregular until you increase the RateSample. > The initial value of X1 is 0 in my program. Since "A" is "-A" > the intial value is positive. > It reminds me E(t)=A*SIN(wt) where w is 2pi radians. > > Regards > Dennis. > '----------------------------------------------- > DIM array(100) > DIM ToneFreq AS INTEGER > DIM SampleRate AS INTEGER > DIM X2 AS DOUBLE > ToneFreq = 440 > SampleRate = 10000 > PI = 3.1415 > A = 5 > X2 = -A * SIN(2 * PI * ToneFreq / SampleRate) OPEN "LIST.TMP" > FOR OUTPUT AS #1 FOR i = 1 TO 100 k = 2 * COS(2 * PI * > ToneFreq / SampleRate) X0 = k * X1 - X2 > X2 = X1 > X1 = X0 > array(i) = X0 > PRINT #1, array(i) > NEXT i > CLOSE #1 > > > ----- Original Message ----- > From: "Andrew Warren" > Sent: Monday, September 27, 2004 6:40 PM > > Harold Hallikainen wrote: > > 1. What's "A"? > > 2. What's the initial value of X1? > > 3. Is there anything else missing? > > > _______________________________________________ > http://www.piclist.com > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist _______________________________________________ http://www.piclist.com View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist