# How to sample audio at Nyquist frequency with MSP430F5438?

I want to:

1. Input an analog audio track

2. Using the microcontroller ADC, convert it to a digital output

3. Then have the microcontollers/boards timer sample the data at selected intervals

4. Resample the "Sampled audio track" at twice the highest frequency content

5. Convert it back to analog as a perfect reconstruction of the initial audio track

Using Fourier Analysis I will determine the highest frequency at which I will sample the track at.

It sounds easy enough and straight forward, but what I need is to program this in C and utilize my MSP430 chip/experimenters board to sample the track. I'm going to be using Texas Instruments CCS and Octave for my programming and debugging. I am using the MSP430F5438 Experimenter Board.

Is C the right language for this? Can I get any examples of how to sample the track at Nyquist frequency using C? What code in C will tell the board to utilize the ADC component? And any recommended information that is similar or that will help me on this project.

• A really cool way to do this is to make a rough PSD meter with an audio crossover, setting sampling frequency to the minimum frequency bin achieving a threshold. This is heavy hardware design, but that is what makes it cool! Note that it would still be inferior to oversampling. Commented Dec 26, 2010 at 19:32
• Interesting thought of how to go about this. Ill look into it and see if its more work then just oversampling the data. Commented Dec 26, 2010 at 21:34
• @Martin, it is definitely more work! ;) Commented Dec 26, 2010 at 21:52
• How much work do you think a ADC, oversampling process, then DAC in c would take? Commented Dec 27, 2010 at 0:13
• ADC and DAC can be hardware peripherals, leaving the micro to do nothing but wait for ADC interrupts to read values from the ADC registers or timer interrupts to write values to the DAC registers -- it's the FFT that can leave it breathless. It is worth learning where to optimize a FFT algorithm (including looking for alternatives) for cycles and memory while staying within your requirements. Commented Dec 27, 2010 at 0:20

You can do your entire project in a desktop PC. In fact, if I had to do it, I would start with the desktop:

1. a .wav file is already sampled at a high frequency, often 44100 or 48000 Hz.
2. determining the highest frequency can be done with an FFT. For prototyping, I would link FFTW.
3. downsampling to an arbitrary frequency is a bit hard, because downsampling involves low-pass filtering. You need to set up a filter for each frequency. Look at libsamplerate and see how it sets up a SINC function to convolve against.
4. Converting back to the original sample rate will involve another low-pass filter. See again libsamplerate.

I believe I would implement this in several passes, for ease of debugging:

1. Get everything working in Matlab or Octave first. Octave has libraries to do all the filtering and Fourier analysis.
2. Get everything working in C on PC, linking FFTW and libsamplerate for the downsampling / upsampling.
3. Rewrite the C code with explicit-width variable types (e.g., int16_t instead of "short") and replace FFTW and libsamplerate with own code so that it compiles standalone.
4. In C for the MSP430 or whatever DSP you've got, write interrupt routines to sample data on the ADC and output it on the DAC. Test that this works, just going from input to output.
5. Take the working code from step 3 and compile it for the MSP430 or whatever. Then wedge it in to the working code from step 4 to operate on the sampled data between ADC and DAC.

This may seem like a lot of steps, but it is much more likely to make a working result than heroically coding everything up in one huge MSP430 application, then trying to debug it on the dev board.

• How would i then use nyquist or oversample it? ADC isnt so much of a problem because they give example code, but chouldnt i just patch the sampling code into the already given ADC code? Commented Dec 27, 2010 at 0:19
• I don't know what you mean by "use nyquist." Please explain. Commented Dec 27, 2010 at 0:28
• sampling it at 2x the highest frequency; though i think just over sampling it would be best (so just say 20000hz instead of 2c highest of (unknown) frequency).. But anyways what your saying is to download sample it first. Commented Dec 27, 2010 at 0:31
• It's usually better to just decide a priori what the highest frequency you need to handle is. For audio, you might choose 20 kHz, since that's around the top end of what people can hear. Then design your system around that: for 20 kHz, you might sample at 48 kHz, so your sampling filter has 4kHz between passband and stopband, making it easier to design. Commented Dec 27, 2010 at 0:36
• I suggest you do your DSP experimentation on your desktop with Octave or Matlab. And start with "blink the LED" on the microcontroller. Get good at both "DSP" and "microcontroller" before you attempt "DSP on microcontroller". Commented Dec 27, 2010 at 1:33

You will have to use the family guide and the Data sheet - found here It will tell you how to setup the ADC and give some basic examples, also TI has sample code for the MSP430 line of chips which includes ADC10 and ADC12 examples.

• I couldnt find the example code form the ones that ti gives you. Commented Dec 25, 2010 at 19:24
• try finding your chip here focus.ti.com/mcu/docs/… Commented Dec 25, 2010 at 20:11
• Ive tried looking though the example but have not found anything useful :( Commented Dec 25, 2010 at 21:40
• you will not find any out of the box code that will work for your project, but it should give you a good place to start coding. Try using msp430x54xA_adc12_05.c (ADC12, Using an External Reference) and msp430x54xA_adc12_07.c (ADC12, Repeated Single Channel Conversions) from slac375a.zip. Commented Dec 25, 2010 at 21:59
• ok will look, and someone suggested this nyquist sampling code. programmers.stackexchange.com/questions/29991/… your thoughts? look at answer 2. Commented Dec 25, 2010 at 22:08

You will find things much easier if you use a proper DSP, I'd use a dsPIC. Microchip has FFT libraries.

• How much do the dsPIC's run for? Can you give me any suggestions? Commented Dec 25, 2010 at 21:39
• It depends on the chip, it could be <\$2. See the Microchip web site. Commented Dec 25, 2010 at 21:44
• What would i need in the way of an experimenters board so i can have a hook up to my computer? Commented Dec 25, 2010 at 21:47
• I'd design a small PCB. Commented Dec 25, 2010 at 22:08
• It would be great if you can give me a suggested link to a board that would be good for my project. i also hear you can simulate data in matlab eliminating the need for a board. Commented Dec 25, 2010 at 22:30

You might want to check out the DSC Starter Kit. It provides you with all of the software and hardware needed to get started with audio recording on a microcontroller. It also has some example programs.

DFT is a little bit more difficult, but there are prebuilt libraries for it. The problem is finding a free one and making sure you have enough ram to do all of the computations needed.

• its not so much audio recording, my current board as a pre build recoding software, its sampling the data that im having trouble with. Commented Dec 25, 2010 at 23:43
• @martin I am not sure what part you are needing help with. The board I linked to had code given to sample. If you want to know how to do an A-D conversion just read what the command is in the datasheet. Commented Dec 26, 2010 at 0:38
• yea i saw it now after reading the examples more thoroughly, but i mainly need help with the sampling process using nyquist theorem in C. Commented Dec 26, 2010 at 0:46
• What I would do is sample as fast as the hardware allows. I would then look for the highest frequency above a certain threshold. Double that and then use that for my sampling frequency. Commented Dec 26, 2010 at 0:53
• Thats what i think ill end up doing. programmers.stackexchange.com/questions/29991/… that one answer of how to sample in nyquist frequency. Commented Dec 26, 2010 at 3:28

A program can only identify frequencies at half its sampling rate. There are two ways to deal with this:

1. oversampling: sample at more than twice the maximum possible frequency (eg. 48kHz > 2x20kHz), then compress the data by low-passing it (decimation) at the highest computed frequency.

2. undersampling: sample at some minimum frequency, then decide to sample at a higher frequency based on the power level in your highest computed frequency bin (FFT term). If it is above a certain threshold, increase the ADC sampling frequency.

The only way to succeed in real-time is with oversampling, as undersampling will have to either skip resampling the parts it has already sampled at too-low a frequency or request it to be replayed.

The linked dev board seems expensive for this application. Is there more to your project? It has code examples of audio sampling and processing: SLAC227 (look in FFT.c).

• Im planing on doing oversampling are my main experiment, then show the effects of undersampling on a 3 tone audio track. The reason i went with that board was 1) It was suggested 2) i can use it for multiple applications in the future. Commented Dec 26, 2010 at 21:32