Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
So, how long have CD's been around now? Since the early eighties, right? Right, in 1982 the first digital audio 5-inch CD discs were marketed - Philips/Sony finalized their standard in 1980, they had begun work in 1969.

So, how long has digital audio been around? Shortly before that, right? Nope, digital audio was born in the early 20th century when Alec H. Reeves invented pulse-code modulation PCM at the International Telephone and Telegraph Co. in France. PCM wouldn't be used until 1962 after the first practical integrated circuits were developed. The first analog sound wave was digitized at 8000 hertz.

But, the notion of digital audio and the notion of 'digitizing' and analog wave has been around since the early 1830's, when Samual Morse was expermenting with telegraph relays.

So, who is Nyquist and what does he have to do with all of this ob-trivia?

Henry Nyquist was a chap that worked at Bell Telephone Laboratories in the early 20th century. In 1928, he discovered that to acurately represent an analog wave digitally, that analog wave needs to be sampled by at least TWICE the amount of the highest perceived frequency that is desired for playback. I mentioned above that the first analog wave was digitized at 8KHz, this means that highest perceived frequency would only be half of that, 4KHz - but this perfect for the human voice, which is contained in the range of roughly 90 to 1200 Hz.

So, what happens when you break Nyquist's Theorem? Frequency aliasing - new frequecies appear and distort the original signal. The same phenoma appears when you watch the blades of a helicopter rotate - you seem to see new 'blades' which rotate the other direction at a much slower frequency.

According to Nyquist's Theorem, if S is the sampling rate, F is a frequency greater than 2S, and N is some integer, then a new sampled frequency is also created at:

    Fa = ± NS ± F

Where Perl Comes In

So, let's demonstrate Nyquist's Theorem with the CPAN module Audio::Wav. Our sampling rate will be 44.1KHz at 16 bits - the CD standard. According to the formula above, if we try to sample a 36KHz wave, we should hear an 8KHz aliased wave:
    S - F = Fa
    44 - 36 = 8
And here is the code to demonstrate. Two.wav files will be created (each one is two seconds - one at 36KHz and the other at 8KHz) - play them back to back in your favorite audio player for a comparison. Have fun, and for an extra bonus, call a friend on your telephone and play the results for them - they won't be able to hear it, because your conversion is being filtered to prevent aliasing.


use strict; use Audio::Wav; use Math::Complex; use constant TWO_PI => pi() * 2; my $wav = Audio::Wav->new(); my $sample_rate = 44100; my $bits_sample = 16; write_out('nyquist_good.wav',8000,2); write_out('nyquist_bad.wav',36000,2); sub write_out { my $filename = shift; my $write = $wav->write($filename, { bits_sample => $bits_sample, sample_rate => $sample_rate, channels => 1, }); add_sine($write,@_); $write->finish(); } sub add_sine { my ($write,$hz,$length) = @_; my $max_no = (2 ** $bits_sample) / 2; $length *= $sample_rate; for my $pos (0..$length) { my $time = ($pos / $sample_rate) * $hz; my $val = sin(TWO_PI * $time); my $samp = $val * $max_no; $write->write($samp); } }
I updated the code with jryan's suggestion, he also told me to remind you folks to turn down your volume before you listen, as an 8KHz sine wave is not a rather pleasing tone. :)


References:

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

In reply to Nyquist's Theorem in Action with Audio::Wav by jeffa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-29 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found