*that*I'll leave for another time, although this blog entry is somewhat related.

I have a circuit which takes a signal from a microphone and feeds it into a PIC microcontroller. This PIC then performs Analogue-to-Digital conversion and stores the data in memory for me to mess with later.

The process of reading in the signal and storing it in memory is known as "sampling". A single piece of recorded data is called a "sample", the rate at which you record the information is called the "sample rate".

Are you following me so far?

The sample rate is super useful to know if you plan on doing anything clever with your data. For my work I need the sample rate to be as fast as possible so I have my PIC recording samples in a loop until its memory gets full. This means I don't actually know how fast this is happening, so I need to measure it.

Read on to find out how I did this, and for some handy MATLAB code!

The first thing I did was to sample something. My microphone/PIC system has a memory of 128 bytes and each sample uses 1 byte of memory. I played a 1kHz tone at it and sent the recorded data along a serial cable to my computer where I logged the results into a text file. When that'd finished I loaded it all into MATLAB so that I could visualise it properly.

Here's a graph of what was recorded:

I'll call the sample rate

*fs*. The sample rate is measured in Hertz (Hz), which is the number of samples recorded per second. I'll call the frequency of the signal I played at the microphones

*fk*, but it was actually 1000Hz in this example. Below is a really rubbish drawing of how these two values relate to the signal I recorded:

The

*x*axis in the diagram is time and the graph shows a signal as it is sampled. Each sample lasts 1/

*fs*seconds before the next sample is recorded. Over the course of one wavelength of our input signal -which lasts 1/

*fk*seconds- there are n samples. SO...

n * 1/

*fs*= 1/

*fk*

or...

*fs*= n*

*fk*

Now we just have the problem of finding what n is. There are several ways of doing this.

The first, and easiest, one is visual estimation. If we go back to the first picture there are just under 2 and a half wavelengths in our memory buffer, let's say 2.4 wavelengths. We also know there are 128 samples in the diagram so.. 128/2.4 will give us an estimation of n: 53.333. Multiplying that by

*fk*gives us an estimated sample rate of 53,333Hz.

Alternatively we could use MATLAB's "data cursor" button, which becomes available when you plot data.

Use the data cursor to select the beginning of a waveform, note down the x coordinate, then select the end of a waveform and do the same. The difference will be the number of samples per waveform.

So here x = 10.

And here x = 69. So n = 69-10 = 59. It's worth doing this a few times to get an average value for n. If n=59 then our sample rate is n*

*fk*= 59*1000 = 59,000Hz.

I thought this seemed rather large, so I wrote a MATLAB GUI which shows you the data you might expect if you sampled a signal

*fk*at sample rate

*fs*. In the GUI,

*fk*and

*fs*can be varied so have a play! There are 2 parts, the gui data and the gui code which need to be saved in the same folder before MATLAB can open them.

When I put in my values for the sample rate and the known frequency input it looks more or less like my original data, a good sign! The GUI can also be used to demonstrate a phenomenon called aliasing, which happens when the input frequency gets close to and above

*fs*/2. Basically, because we're taking samples instead of recording the actual sound then signals start to resemble each other even though they're not the same. This wikipedia image of aliasing illustrates why this happens.

## No comments:

## Post a Comment