3
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$
    – tyblu
    Dec 26, 2010 at 19:32
  • \$\begingroup\$ Interesting thought of how to go about this. Ill look into it and see if its more work then just oversampling the data. \$\endgroup\$
    – Martin
    Dec 26, 2010 at 21:34
  • \$\begingroup\$ @Martin, it is definitely more work! ;) \$\endgroup\$
    – tyblu
    Dec 26, 2010 at 21:52
  • \$\begingroup\$ How much work do you think a ADC, oversampling process, then DAC in c would take? \$\endgroup\$
    – Martin
    Dec 27, 2010 at 0:13
  • \$\begingroup\$ 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. \$\endgroup\$
    – tyblu
    Dec 27, 2010 at 0:20

5 Answers 5

4
\$\begingroup\$

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.

\$\endgroup\$
25
  • \$\begingroup\$ 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? \$\endgroup\$
    – Martin
    Dec 27, 2010 at 0:19
  • \$\begingroup\$ I don't know what you mean by "use nyquist." Please explain. \$\endgroup\$
    – markrages
    Dec 27, 2010 at 0:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Martin
    Dec 27, 2010 at 0:31
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – markrages
    Dec 27, 2010 at 0:36
  • 1
    \$\begingroup\$ 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". \$\endgroup\$
    – markrages
    Dec 27, 2010 at 1:33
2
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ I couldnt find the example code form the ones that ti gives you. \$\endgroup\$
    – Martin
    Dec 25, 2010 at 19:24
  • \$\begingroup\$ try finding your chip here focus.ti.com/mcu/docs/… \$\endgroup\$
    – jsolarski
    Dec 25, 2010 at 20:11
  • \$\begingroup\$ Ive tried looking though the example but have not found anything useful :( \$\endgroup\$
    – Martin
    Dec 25, 2010 at 21:40
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – jsolarski
    Dec 25, 2010 at 21:59
  • \$\begingroup\$ ok will look, and someone suggested this nyquist sampling code. programmers.stackexchange.com/questions/29991/… your thoughts? look at answer 2. \$\endgroup\$
    – Martin
    Dec 25, 2010 at 22:08
2
\$\begingroup\$

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

\$\endgroup\$
9
  • \$\begingroup\$ How much do the dsPIC's run for? Can you give me any suggestions? \$\endgroup\$
    – Martin
    Dec 25, 2010 at 21:39
  • \$\begingroup\$ It depends on the chip, it could be <$2. See the Microchip web site. \$\endgroup\$ Dec 25, 2010 at 21:44
  • \$\begingroup\$ What would i need in the way of an experimenters board so i can have a hook up to my computer? \$\endgroup\$
    – Martin
    Dec 25, 2010 at 21:47
  • \$\begingroup\$ I'd design a small PCB. \$\endgroup\$ Dec 25, 2010 at 22:08
  • \$\begingroup\$ 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. \$\endgroup\$
    – Martin
    Dec 25, 2010 at 22:30
1
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ its not so much audio recording, my current board as a pre build recoding software, its sampling the data that im having trouble with. \$\endgroup\$
    – Martin
    Dec 25, 2010 at 23:43
  • \$\begingroup\$ @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. \$\endgroup\$
    – Kellenjb
    Dec 26, 2010 at 0:38
  • \$\begingroup\$ 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. \$\endgroup\$
    – Martin
    Dec 26, 2010 at 0:46
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kellenjb
    Dec 26, 2010 at 0:53
  • \$\begingroup\$ Thats what i think ill end up doing. programmers.stackexchange.com/questions/29991/… that one answer of how to sample in nyquist frequency. \$\endgroup\$
    – Martin
    Dec 26, 2010 at 3:28
0
\$\begingroup\$

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).

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Martin
    Dec 26, 2010 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.