## Sampling rate + reconstruction question

22 posts / 0 new
Author
Message

If I generate a 20Khz digital sine wave with a microcontroller, outputing each sample at a rate of 44.100Khz, I don't understand how I can get a sine wave since I'm going to have only 2 samples for each period. How can a sine wave be reconstructed with only 2 samples by period ? With an LP reconstruction filter ? But if it is not a sine but a triangle, a square, a saw... I think the LP filter will distord the signal.

The sample rate of a CD is 44.100KHz and we can ear sound of 20Khz in this CD. Same question that above, how can we ear a perfect sine (or triangle,etc) in this CD since there are only 2 samples every period ?

You are right while Nyquist says a minimum of 2 for a "shaped" wave like sine its generally considered you need at least 10 times over sampling for decent reconstruction. Same reason people use 100MHz+ scopes when exploring things like 16MHz AVR clocks etc.

I recommend that you watch https://xiph.org/video/vid2.shtml (go to 3:36, the "stairsteps" chapter).

By the way, we can't even hear the difference between a 20kHz sine wave and a 20kHz square, sawtooth, or triangle wave. All of the harmonics are above the maximum frequency we can hear.

Also, in a properly-configured A/D system with a 44.1kHz sample rate, any square wave or triangle wave at or above 7350 Hz (22050/3) will be low-pass filtered and will become a sine wave because a square wave and a triangle wave have only odd harmonics (1, 3, 5, 7, ...). The same goes for a sawtooth wave at or above 11025 Hz (22050/2) because a sawtooth wave has both even and odd harmonics (1, 2, 3, 4, ...).

Lots of us can't even hear 20kHz after a certain

age.  The kids know this and apparently have

used high-frequency tones to share answers to

exam questions without the proctor knowing.

--Mike

jowell88 wrote:

...outputing each sample at a rate of 44.100Khz.But if it is not a sine but a triangle, a square, a saw... I think the LP filter will distord the signal.

That's the thing though, you cannot make a 20kHz square wave with a 44kHz sample rate.

A 20kHz square wave contains components at a frequency of 20kHz, 60kHz, 100kHz, 140kHz, 180kHz,....

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

It is tough to visualize how you get a sine wave from slightly MORE than 2 samples / period when you think in the time domain.

It is easier to understand when you think in the frequency domain.

There are entire books written on digital signal analysis.

Short (really short) version:

A sine wave of 1 KHz, for example, has a frequency component (signal energy), at only 1 kHz.

A Square Wave of 1 KHz can be viewed as being made up of an infinite sum of sine waves.

The sine waves are all harmonics of the base frequency.

So a 1 KHz square wave is made up of sine waves of 1 KHz, 2 KHz, 3, 4, 5, 6.... KHz.

For a square wave all of the even harmonics, 2, 4, 6...kHz have an amplitude of 0.

So the square wave is actually made up of just odd harmonics, i.e. sine waves of 1, 3, 5, 7... KHz.

The amplitude of each harmonic decreases as the harmonic gets higher.

So the square wave is made up of lots of 1 KHz sine wave.

A bit less of a 3 KHz sine wave,

A bit less of a 5 KHz sine wave...

For the odd harmonics the actual amplitude of the harmonic is X = 2/(Pi x n)

where n is the harmonic number.

If you look at one sine wave, at 1 KHz, it doesn't look much like a square wave.

If you took an op-amp and added 1, 3, 5, and 7 KHz sine waves, with the amplitudes set as above, it would look like a pretty good square wave.

If you added up the first 20 components it would look like a very good square wave.

If you added up an infinite number of components, the square wave would be perfect, with o rise time and 0 fall time and no overshoot or undershoot.

Same concept applies to triangle, speech, EKG, and any other PERIODIC waveform.

You can construct the waveform as a sum of sine waves, each with the correct frequency, phase, and amplitude.

So, now work backwards.

Take a complex, periodic, (i.e. repeating) waveform and look at it on a spectrum analyzer.

The signal is made up of lots of energy spikes, each representing a sine wave of a given frequency and amplitude, and part of the overall signal.

Now run that signal through a LPF and you start losing the higher frequency components.

As your lower the frequency of the LPF, fewer and fewer components of the original signal get through, and you see a distorted version of the original signal as the output of the filter.

Keep lowering the LPF cutoff, and eventually you only see one sine wave, at the same frequency as the original base frequency for the complex, periodic waveform you fed into the filter.

Now, back to your original question.

View, in the time domain, (e.g. O'scope image, V vs Time), your signal with two samples.

For the sake of argument, assume one sample hit on the positive peak of an imaginary sine wave, and the other sample hit on the negative peak of the sine wave.

You see a series of High and low dots on the O'scope, not the sine wave.

Feed that through a LPF so as to only pass the base (fundamental) frequency of all of the individual frequency components that make up the periodic series of dots waveform.

What you see is the sine wave that is the base, (fundamental) frequency of the dots.

The actual Shannon-Nyquist Theorem says that you have to sample at > twice the highest frequency component of the signal being digitized in order to adequately be able to reproduce the signal, (not at twice the highest frequency component of the signal).

In the example above, we pretended to take two samples of the sine wave and they fell at the + peak and the neg peak of the signal.

Shift the sampling time a little bit and one could have the two samples occurring at the zero crossing points for the sine wave, and the two samples would both have a value of 0.

It is hard to recreate a signal with two samples of value 0.

Hence one has to sample at >, not >=, the highest freq component of the signal.

In order to view the original signal from a series of samples, you have to decide how many dots to use to draw the picture in the time domain.

This is a classic parameter of interest when purchasing a digital O'scope.

It is not only important what the bandwidth of the scope is, but also what its sampling rate is.

Drawing a sine wave with 100 samples looks much more like a sine wave than one drawn with 10 samples, (5 dots for the positive hump, 5 dots for the negative hump).

I don't have a photo of a "slightly" greater than two samples / period digital signal being fed through a LPF.

I do have the image below that will perhaps help.

The top waveform is about 8 samples, (or whatever it was), of a sine wave.

It looks very stair-stepish.

With a (really) good imagination one might guess that it represents a (highly distorted) sine wave.

Feeding it thought a LPF gave the lower image, a really nice looking sine wave.

All of the steps and sharp edges are gone.

The higher frequency components that make up the upper waveform have been removed, leaving just the base, (fundamental) frequency component of the signal, the sine wave seen in the lower trace.

Now, instead of having ~ 8 "Dots" or Levels or Samples making up the upper waveform, imaging you just had your slightly more than two Dots, Levels, or samples making up the waveform.

It would look either like the series of high and low dots, or look like a square wave if the lines were connected.

Feed it though the same LPF and you will again see the terrible looking "sine wave" come out as a really nice looking sine wave.

(The image, BTW, was from an Xmega DDS generating a tunable drive signal for an ultrasound transducer, to then find its resonant freq)

JC

DocJC wrote:

If you added up an infinite number of components, the square wave would be perfect, with o rise time and 0 fall time and no overshoot or undershoot.

There is an effect called Gibbs Phenomenon where

the amount of overshoot remains about 9% the size

of a step regardless of the number of components

used to approximate the square wave.  Adding more

terms only causes the ringing to attenuate faster.

--Mike

EDIT: grammar

Last Edited: Tue. May 7, 2019 - 08:03 PM

I suspect that something else also happens when you have 44KHz sampling trying to create a 20KHz signal. The samples "slide" through the signal. I think that the brain uses that, somehow, to improve the apparent quality as perceived by the brain.

This has been a lot written in the last few years about "brain plasticity". One of the incredible examples of this involves people who became blind as a teenager or older. Researchers have built arrays of photo sensors (32 x 32 I think) and coupled them to a similar array of vibrators that the blind person could wear on their chest or back. After some (not terribly long) practice time, the blind people were often able to "see"; anecdotal reports suggested that scenes were perceived in much higher resolution than should be possible with a 32x32 sensor array.

So, when it comes to human perception, Nyquist does not seem to tell the whole story.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

There is an effect called Gibbs Phenomenon

I had heard of that, and totally forgotten it... Until today!

So that's why my upper O'scope trace, above, has overshoot and ringing!

And here I thought it was just poorly adjusted Scope Probes and crappy equipment!

JC

jowell88 wrote:

If I generate a 20Khz digital sine wave with a microcontroller, outputing each sample at a rate of 44.100Khz, I don't understand how I can get a sine wave since I'm going to have only 2 samples for each period. How can a sine wave be reconstructed with only 2 samples by period ? With an LP reconstruction filter ? But if it is not a sine but a triangle, a square, a saw... I think the LP filter will distord the signal.

The sample rate of a CD is 44.100KHz and we can ear sound of 20Khz in this CD. Same question that above, how can we ear a perfect sine (or triangle,etc) in this CD since there are only 2 samples every period ?

As other have explained, the Low pass filter actually helps to reconstruct the wave form.  I was curious enough to build a 2nd order and 4th order Low Pass Filter and then apply the output of a DAC to each.  In this case, the LPF had a cutoff of 16KHz while the sample rate was 32KHz  What you see below are a sequence of scope pictures of DAC output in the top row and filter outputs in the next two rows.  The top traces in each screen shot are the ones applicable to your statement/question (the bottom traces are error signals of the PWMDAC).

What you see in the upper trace of the top row of 3 screen shots is output of a DAC at ~16KHz, (2 samples/per maximum frequency), ~8KHz (4 samples per max freq) and ~4KHz  (8 samples/max freq).

The upper traces of the second row of screen shots is the output of the 2nd order Butterworth filter while the upper traces of the third row of screen shots is the output of the 4th order Butterworth filter.  You can see how the filter reduces the upper harmonics of the square wave to produce a close proximity to a (delayed) sine wave.

Edit: Fill in missing words

Last Edited: Wed. May 8, 2019 - 01:50 PM

Christop, the video was really instructive!

If you use a 44 kHz sampler for sampling a 20 kHz signal, or any signal bandlimited below 22 kHz, you can approximate the original signal by truncating the following formula:

Last Edited: Sun. May 12, 2019 - 10:06 PM

--Mike

jowell88 wrote:

If I generate a 20Khz digital sine wave with a microcontroller, outputing each sample at a rate of 44.100Khz, I don't understand how I can get a sine wave since I'm going to have only 2 samples for each period. How can a sine wave be reconstructed with only 2 samples by period ? With an LP reconstruction filter ? But if it is not a sine but a triangle, a square, a saw... I think the LP filter will distord the signal.

The sample rate of a CD is 44.100KHz and we can ear sound of 20Khz in this CD. Same question that above, how can we ear a perfect sine (or triangle,etc) in this CD since there are only 2 samples every period ?

The really important thing to understand is that if you sample at 44,100Hz you can't accurately represent a 20,000Hz triangle, square or saw wave.

Frequency is the rate of change of velocity over time. A sine wave has constant rate of change. Imagine a car driving around in a circle - all you need to do is keep the wheel turned in one direction. The angle of the wheel never changes.

Now imagine trying to follow a waveform shaped road in the car. The higher the frequency, the sharper the curves. And you can only turn the wheel so far, there is a limit to how hard you can turn. If the road is higher frequency than your car can turn it can't follow the road.

Now imagine a square wave road. The turns are 90 degrees. Your car can't do 90 degree turns.

So actually with digital sampling you can't represent a perfect square wave. Electrically it's impossible too, like your car every circuit has some limit to how fast it can "turn".

Well, Nyquist says you need to sample 2 times the max frequency you are interested in.

If you're trying to reproduce a sine wave at x hertz, you're interested in frequencies WAY beyond x so you need lots more than 2x samples.

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead.

One important factor, often unknown to the public that is rarely mentioned, is that you need to sample at least twice the bandwidth of the band-limited signal, not merely the highest freq component. Generally this amounts to the same thing, since we typically go down to DC.  However, say you have an RF signal with all components from 200.001 MHZ to 200.004 MHz...you'd think you'd need to sample around 400MHz.  But with this 3000Hz bandwidth signal, you only need to sample closer to 6000Hz using sampling translation!  So your ADC can be much slower, but much more timing stringent. In practicality, that may be easier said than done.  There are a lot of details and restrictions on usable sampling rates to get the sampled spectrum to butt up properly.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. May 21, 2019 - 04:25 PM

avrcandies wrote:

However, say you have an RF signal with all components from 200.001 MHZ to 200.004 MHz...you'd think you'd need to sample around 400MHz.  But with this 3000Hz bandwidth signal, you only need to sample closer to 6000Hz using sampling translation!

But before you do that, use an analog bandpass filter to

attenuate everything not in the 200.001 - 200.004 MHz

range of interest.  Also your sampling must be extremely

precise in terms of the timing.  Any jitter in the sample

clock and you will get garbage.

--Mike

Torby wrote:

Well, Nyquist says you need to sample 2 times the max frequency you are interested in.

Indeed, but with the important caveat that it is 2x the max frequency of the SINE wave you are interested in.

If you have a 1kHz square wave then you need to sample at a lot higher than 2kHz to get something out that resembles the input.

mojo-chan wrote:

Torby wrote:

Well, Nyquist says you need to sample 2 times the max frequency you are interested in.

Indeed, but with the important caveat that it is 2x the max frequency of the SINE wave you are interested in.

If you have a 1kHz square wave then you need to sample at a lot higher than 2kHz to get something out that resembles the input.

Um. Isn't that what I said?

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead.

Torby wrote:

Well, Nyquist says you need to sample 2 times the max frequency you are interested in.

If you're trying to reproduce a sine wave at x hertz, you're interested in frequencies WAY beyond x so you need lots more than 2x samples.

If you're trying to reproduce a sine wave at x hertz, you're only interested in frequencies as far as x, so 2(and a bit)x samples.

If you're interested in anything *other* than a sine wave, then the significant harmonics will always be some multiple of x and if you need to reproduce it, you'll need 2 * x * that multiple to recreate it.

BUT... if you have 'standard' hearing to 20kHz (which probably very few have, though some will go higher) for any frequency greater than 10kHz you can only hear the fundamental. For frequencies between 5kHz and 10kHz you can hear the first harmonic. Between 2.5kHz and 5kHz you can hear the first and second harmonic, and so on. At 20Hz, the bottom of most people's hearing, you can hear nine harmonics (40Hz, 80Hz, 160Hz, 320Hz, 640Hz, 1280Hz, 2560Hz, 5120Hz, 10240Hz - the 20480Hz is out of range).

Which is why sampling a 20kHz signal at 44kHz works; it can be reconstructed back to the only component you can hear - the fundamental.

Neil

Which is why sampling a 20kHz signal at 44kHz works; it can be reconstructed back to the only component you can hear - the fundamental.

If interested only in "hearing" the signal (rather than scoping), there are a whole bunch of tricks that have been used.

For example your ear has poor sensitivity to phase information (or phase reconstruction can be allowed to be poor).  Your sensitivity to soft notes in presence of loud notes allow them to be more distorted.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Yes, but that's largely a function of the data compression algorithms in most audio storage and transmission systems: if the target audience can't hear it, don't bother saving/transmitting it. C.f. G711, MP3 layer 2 etc.

As you point out, the phase response of the ear is poor - which is why you can transform an audio chunk into the frequency domain and back, which kills the phase information, and it still generally sounds ok (with a short enough sample). However, phase information between the ears is used for some parts of positioning (and the shape of the various bits of the external ear) as well as relative volume.

Sensitivity to soft notes (or lack of sensitivity) in the presence of louder is called 'masking' and is again an important part of MP2 and MP3 layer 2 audio encoding. It's easy to see what will be masked when you have the fourier transform to hand.

But the original point stands: if you wish to inspect a waveform, you *must* sample at least twice as fast as the highest frequency component in which you're interested; this usually means eight or sixteen times the the highest fundamental - for the third or fourth harmonic. Though of course harmonics go to infinity...

Neil