Go Down

Topic: Generating high frequency tones (Read 2844 times) previous topic - next topic

edan

Hi all,
I want to generate high frequency tones (20kHz and up). Looking at programs like the Arduino DDS signal generator (http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/) though, it looks like it's limited to a 32kHz sampling rate, which means a 16kHz max output frequency. Why is this?

robtillaart


Think it is because it generates a sine wave , if you want a square wave you can easily go much higher:

Code: [Select]

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
    digitalWrite(13, HIGH);
    // add microdelay or delay here
    digitalWrite(13, LOW);
    // and here
}


by using direct port manipulation and/or timers you can even go higher.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Nick Gammon

#2
May 17, 2012, 10:58 pm Last Edit: Jun 04, 2012, 11:52 pm by Nick Gammon Reason: 1
If you are talking about generating sinewaves then you need to look at how audio sampling works. To generate simple square waves, you can go up to 8 MHz as robtillaart says.

From this page:

http://en.wikipedia.org/wiki/Sampling_%28signal_processing%29



The above image shows the general idea. We need to sample the sound we are trying to produce (eg. a sine wave) at at least twice the frequency we desire, see:

http://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem

So to get 20 kHz we need 40 kHz samples (CDs BTW sample at 44.1 kHz).

That is, a sample every 25 mS. uS.

(edit) Corrected on AWOL's advice.

Now looking at the spacing of the dots vertically the samples need to be able to reproduce a range of values, or we are back to having a square wave. Say, 256 different values (as in the linked page you gave). That is in fact only "8-bit sampling". BTW the Wikipedia page says that "Eight-bit audio is generally not used due to prominent and inherent quantization noise". So we really should use more than 8 bits.

But sticking with 8 bits for now, to generate the 256 different voltage levels required, we need to use PWM (pulse width modulation) where a square wave is produced with a variable duty cycle.
See: http://en.wikipedia.org/wiki/Pulse-width_modulation



For example a duty cycle of 128 would be "half voltage" and duty cycle of 64 would be "quarter voltage" and so on (assuming this is out of 256).

Now for the internal timer to do this, it has to count up to 256. And the fastest it can do that is at the clock rate which is 62.5 nS.

Code: [Select]
62.5 nS * 256 = 16 uS.

So how many times can we count up to 16 uS in one second? 1/0.000016 = 62500. Therefore the absolute maximum sampling frequency, with a 16 MHz clock, and 8 bit samples, would be 62.5 kHz.

There is another factor ... for the individual samples to be "in phase" the authors of that sketch have used "phase correct" PWM mode. This mode centers the "on" cycle, rather than having it at one edge. But it takes twice as long to do phase-correct PWM mode, because the timer counts up, and then down again. So we have to divide the figure we got by 2.

Code: [Select]
62.5 kHz / 2 = 31.25 kHz.

So that's why the maximum sample rate is around 32 kHz. Can't argue with the laws of physics. ;)

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

claudiu_mirescu

I am also a newby in using Arduino and I would like to ask you what is the voltage differece between HIGH and LOW in the above sequence? What if I would like to generate a signal that gows below 0, like +5v/-5v, instead of +5v/0v? What should I use instead of HIGH And LOW?

Nick Gammon

You can't, directly. The Arduino operates between 0V and +5V.

You would need some external circuitry, with a negative power supply.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

CrossRoads

"What if I would like to generate a signal that gows below 0, like +5v/-5v, instead of +5v/0v? "

You could take the output signal and pass it thru a capacitor. The cap prevents DC levels from passing thru. So a 0/5V signal would in effect become a +/-2.5V signal.
If you wanted +/-5V then you need to amplify that signal, or buffer (or amplify) the signal with a part that is powered from +/-5V.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AWOL

Quote
So to get 20 kHz we need 40 kHz samples (CDs BTW sample at 44.1 kHz).

That is, a sample every 25 mS.

Ahem!
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon


Quote
So to get 20 kHz we need 40 kHz samples (CDs BTW sample at 44.1 kHz).

That is, a sample every 25 mS.

Ahem!


Er, every 25 uS. <evilgrin>
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Nick Gammon





You know, looking at this diagram that I got from Wikipedia, I'm starting to doubt that the red line has anything to do with the pulses shown by the blue line. What do you think?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dxw00d

I think it's a specific example, rather then a generic PWM signal. The text associated with the image says
Quote
An example of PWM in an AC motor drive: the phase-to-phase voltage (blue) is modulated as a series of pulses that results in a sine-like flux density waveform (red) in the magnetic circuit of the motor. The smoothness of the resultant waveform can be controlled by the width and number of modulated impulses (per given cycle)

I guess it depends on what 'AC motor drive' means.

Nick Gammon

Between the 5 and 10 mS mark the blue line is negative, but the red line is positive. How does that work? Unless it is out of phase because of the smoothing capacitor.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

CrossRoads

You are misinterpreting. The Redline is changing in level by the width of the blue pulse.
Positive pulses, the level goes up.
Negative pulses, the level goes down.
The amount the level is changing is determined by width of the pulse.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up