Discussion in 'Embedded Systems and Microcontrollers' started by l_nash, Jul 8, 2010.

1. l_nash Thread Starter New Member

Jul 8, 2010
3
0
Hi,
I have a doubt regarding sampling frequency to be used for signals.
Nyquist Theorem says that sampling frequency be greater or equal to the maximum freq u want to measure.
Say i have a 10KHz signal and I sample it with an ADC at 48KHz.
For every cycle of a sine wave i will have 4.8 samples. Correct me if I am wrong, this will not be a proper smooth re construction. The reconstructed wave looks more like a triangular wave with sharp edges!

Is there any relation that needs to be followed for proper reconstruction of such signal?

Thank u

Apr 20, 2004
15,815
282
3. Syniva New Member

Jun 21, 2010
25
0
As best I know (which may not be correct, but here it is anyway) it works like this:

Choose a sampling rate that is four times the highest frequency you need to record. Nevermind what the audio people tell you about using twice the highest frequency you want to record. Twice the frequency is correct only when your recorded signal includes both sine and cosine data, which is true only in mathematics. Digitally sampled waveforms contain only sine data, and so you need four times the sampling rate if you want accurate results. Using only double the sampling rate works well enough that people tend not to notice the difference, but if you're doing something like sampling EEG data for software analysis, it matters.

On input, you need a low-pass filter to remove frequencies that your sampling rate is not able to record, because they will distort your recorded signal. Frequencies below 1/4 the sampling rate are ideally unaltered, while frequencies above 3/4 the sampling rate are ideally eliminated. What you do with the frequencies in between is a matter of what is best for your project. For the most accurate digital recording, you want a sharp cutoff at 1/4 the sampling rate. For high quality audio, a sharp cutoff at 1/2 the sampling rate allows higher (although not so accurately recorded) frequencies into the recording which will make it sound better, while a gradual cutoff from 1/4 to 1/2 the sampling rate is an easier filter to build and it most filters the most inaccurately recorded frequencies while least filtering the least inaccurately recorded frequencies. If the digital recording will be used only for Fourier analysis, where you can simply ignore garbage data above 1/4 of your sampling rate, you can use an even simpler filter that passes everything below 1/4 and eliminates everything above 3/4, which will allow frequencies between 1/2 and 3/4 to alias to frequencies between 1/2 and 1/4, but your Fourier analysis can just ignore that.

On output, similar rules apply. Everything above 1/2 your sampling rate is just an artifact of the digital sampling process, and so should be filtered out since it wasn't in your input signal and technically isn't in your recorded file either (although it will appear to be there if you look at it with software since software won't display it correctly). You may also want to filter frequencies above 1/4 of your sampling rate, depending on whether or not you want those frequencies in your output.

Like in the "triangle.png" attachment below? That's the software not displaying the waveform correctly. All software I'm aware of will do that, unfortunately.

To display it correctly, you need to apply the low-pass filtering as would be done when reproducing the waveform with a correct DAC circuit. The "sine.png" image shows what you get when you increase the sampling rate 100 times, then run it through a low-pass filter to remove frequencies greater than 1/2 the (original) sampling rate, like I described above. As you can see, it looks just like the original sine wave.

Here's the code that created the two files:

Code ( (Unknown Language)):
1. #!/usr/bin/perl
2. use constant PI => 4 * atan2(1, 1);
3.
4. open RAW, '>', 'input.raw';
5.   for (\$i = 0; \$i < 100; \$i++) {
6.     print RAW pack('s', 16384 * sin(2 * PI * 10000 * \$time));
7.     \$time += 1 / 48000;
8.   };
9. close RAW;
10.
11. `sox -t raw -c 1 -r 48000 -s -2 input.raw -t wav input.wav`;
12. `sox input.wav -r 4800000 output.wav`;
The "input.wav" creates the file showing the "problem" you're talking about. (It isn't really a problem, like I say, the data is correct, it is simply displayed incorrectly by everything.) The "output.wav" shows what you'll get when passing the waveform out an DAC essentially converts it to an infinite sampling rate (a.k.a. analog) and then your low-pass filter is applied (which the sox command does automatically for sample rate conversions). The result looks just like your original signal, and it will work as long as your original signal contains no frequencies above 1/4 of your sampling rate.

File size:
1.6 KB
Views:
31
File size:
1.9 KB
Views:
33
4. l_nash Thread Starter New Member

Jul 8, 2010
3
0
Thanks for the insight. Exactly what i was looking for

5. l_nash Thread Starter New Member

Jul 8, 2010
3
0
Hi Syniva..
I had this doubt while looking into audio application (freq <20Khz)
My f-sampling is standard 48khz.
I observed that triangular wave shape for the sampled sinewave signal.

Solution would be to oversample my input signal and implement a LPF with fsampling/2(24khz) cut off to properly get my sine wave output rather than triangular ouput. Is this correct?

Another question i have is that, the frequencies between 20k & 24k, will they alias into my audio range of frequencies?

Thanks

6. Syniva New Member

Jun 21, 2010
25
0
The triangle wave you see is just an artifact of incorrect rendering of the data, not a problem with the data itself, and so you do not need to alter your recording process at all (assuming there is no other problem with it).

When you display PCM data, you essentially have to convert the sample rate of your data to the "pixels per second" of your display. The problem is that there are good ways to do sample rate conversions and there are bad ways to do them. Everyone uses the bad ways when displaying PCM data. So you'll see these triangle waves, or sometimes square waves or staircase waves if different but equally bad methods are used. The actual data represents a sine wave when viewed in a mathematically correct way, and it would appear that way on the screen if a better sample rate conversion were used.

For a sampling rate of 48 kHz, you don't have to worry about aliasing until frequencies rise above 24 kHz.

7. lmartinez Active Member

Mar 8, 2009
224
6
The statement you posted above is not accurate. Please review the following link:

https://redwood.berkeley.edu/bruno/npb261/aliasing.pdf