# [chuck-users] demonstrating the sampling theorem in Chuck

Kassen signal.automatique at gmail.com
Fri Oct 19 00:04:20 EDT 2007

```Hi, Jacob!

> As a first step into the wonderful and time consuming world of DSP<=>Chuck
> I want to make audible the effect of disregarding the sampling theorem.
>
Breakin' the law! :¬)
Very good!

So all I'm trying to do at the moment is creating a simple down-sampler
> without any proper filtering. As it is said in the sampling theorem this
> should not create any distortion as long as my new sampling rate is at least
> twice as high as the highest sine wave in my input signal.
>

That's right. As long as you're sampling at at least twice the frequency of
the bandwith of the signal the signal can be completely re-constructed. The
easy way yo guarantee that is to chop off a bit of the bandwith with a LPF
but you can indeed also make sure the signal fall in that range. So far so
good.

>
> By using a common sample-rate like e.g. 44100 Hz in Chuck this should
> satisfy the sampling theorem. I guess the reason, why I doesn't get a nice
> 440-sine wave here is because I have to reconstruct the sine wave after the
> down sampling with the remaining samples, as the output cannot know that
> they are part of a proper sine wave. Is this right? If yes, could someone
> give me a hint how to realize something like this?
>

Yes, that's right. The sampling theorem says the signal can be completely
recovered, however, it doesn't say you can "recover" it by simply listening
to the values as a train.

What's happening is that you are creating a wave with steps in the amplitude
and such steps will have harmonics, some within the range below 22.05KHz,
some above. Fortunately this is simple to solve, all you need is a
brick-wall lowpass filter with a linear phase response set to cut off at
your Nyquist. Unfortunately, those don't grow on trees.

You can try various techniques to approximate one, I'd start by simply
chucking my Step into a lowpass filter ( LPF ) and seeing how that affects
matters. That's not going to cure all of your woes but it will greatly
decrease them. From there on it's diminishing returns. I don't think you
will realize true mathematical perfection but with some reading up on DSP
you could get close to what most ears stop finding objectionable... Or you
might find cases in which you like the artifacts better then the perfection.

Happy ChucKing!
Kas.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20071019/e119bcff/attachment.htm
```