Pages: [1]   Go Down
Author Topic: Creating a sinewave with an Arduino Duemilanove  (Read 1759 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi guys,

iam a bit confused that is because i ask this question.
for the university i should build a sine wave generator using an arduino board and pulse width modulation.
so i know what pwm is and i know what a sine wave should look like  smiley
the question is do i need a table to store the sine wave or is there another possibilty to generate sine values on demand?
and if i use a table is it the same as direct digital synthesis?
do i have to live with the disadvantage of dds when i want to speed up the frequency that the tone gets ugly, beacause iam missing a lot sine values?
is this a problem? is it recognizeable?
i hope you know what i mean.
i want to control the frequency of the sine wave without getting a bad audible result by leaving out multiple sine values.

greetings
peter
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will always leave out a terrible amount of sine values!
What is more, you will always have a bad amplitude discretisation using PWM.
The worst thing of all is, that this is really DDS, which seems to disturb you for some reason!?

Before any of your questions can be answered in a sensible way, please state the required frequency range and acceptable distortion. You mention Audio... There is something like e.g. the HiFi standard....

I should also recommend you just read a little bit about DDS....

« Last Edit: September 13, 2010, 07:40:13 pm by mpeuser » Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the question is do i need a table to store the sine wave or is there another possibilty to generate sine values on demand?

You can generate a sinewave using a digital resonator structure (a.k.a. IIR oscillator, IIR resonator). If you're not familiar with DSP, then perhaps stay away smiley  If you are, I am talking about a two-pole (complex conjugate) system with poles right on the unit circle so it is marginally stable. Kick it and it oscillates. The angle of the poles determines the oscillation frequency and because it's second order the implementation equations are pretty simple.

You could generate sinusoid values on-the-fly using Taylor series and/or CORDIC algorithms.

If you do stick with the simpler lookup table, at high frequencies you can improve the "ugly" factor by interpolating between adjacent values in the table. Linear interpolation is pretty good, quadratic will be even better.

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons
Logged

CT, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 446
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, DSS is the way to go if you wanted to create sinewave. Take a look at this post (http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/) to see how it is done.
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Direct Digital Signal Synthesis is generally shortcut to DDS; but I do hate three letter acronyms and also get confused from time to time..  DDSS would have been much better..

P.S. I do like the "flying filter" in the picture
« Last Edit: September 13, 2010, 08:30:51 pm by mpeuser » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your answers.
i read a bit more about dds and i altough wnat to understand the program which is posted under http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/ (thanks for that, saw it before, but didn't really pay attention to that).
the only problem i have is why is the interupt used to increment the accumulator so the next value out of the sine table can be loaded. isnt it possible to just increment in the while loop?
i didnt get the timing thing right...

greetings
peter
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 515
Posts: 31567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is why is the interupt used to increment the accumulator

So that the increment is timed to the sample rate. If you did it in the loop it wouldn't be synchronised.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok now i have a working arduino sketch and build the circuit on a breadboard.
now i want to control the frequency with a potentiometer.

there is a lin in the code like this dfreq=analogRead(0); // reading on analog pin 0

so my question is, the value is written every second, but i want to have a continuous frequency control, so i got rid of these lines

if(icnt1++ == 10) {

}

and incremented c4ms++ every time the interupt gets called, instaed of increment it every 4 milliseconds.
but the output sounds strange.

http://webuser.uni-weimar.de/~atix5565/arduino/test_sound.wav (dont watch the first 2 seconds)

there are these interupt sound.
how can i get rid of them?

thanks for your answers.
greetings peter.
Logged

Portugal
Offline Offline
God Member
*****
Karma: 5
Posts: 962
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thake a look here:
http://www.scienceprog.com/avr-dds-signal-generator-v20/
and
http://www.myplace.nu/avr/minidds/index.htm
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 515
Posts: 31567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how can i get rid of them?

Same way you caused them, don't read the analogue port so often.

There is too much going on for the processor to cope with. I assume that is why the original coder scaled back the frequency of analogue reads.

You could find another way of reading the pot like replacing it with a rotary encoder and only reading it when it changes. Or feeding the pitch control information in from a PC or other arduino.
Logged

Pages: [1]   Go Up
Jump to: