Pages: [1]   Go Down
Author Topic: Generating high frequency tones  (Read 1822 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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.
Logged

Rob Tillaart

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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. smiley-wink

« Last Edit: June 04, 2012, 04:52:09 pm by Nick Gammon » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

You would need some external circuitry, with a negative power supply.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 444
Posts: 23861
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

"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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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>
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 444
Posts: 23861
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, mS, uS, whatever it takes smiley-wink
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 444
Posts: 23861
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: