views:

153

answers:

5

Hey Stackoverflow;

I currently have an array full of data which from what I believe is the Amplitude of my wave file. It is currently at a low -32768 and at a high 32767.

I also have the SampleRate which was 16,000hz.

My understanding of sound isn't very good; does anyone know from this how I can calculate the Frequency?

Help greatly appreciated,

Monkeyguy.

+2  A: 

This is just what I remember from physics, and I'm definitely no music expert, either.

Unless it's a recording of a constant tone, it probably doesn't have a single frequency. Each tone has a different frequency, which is why they sound different. There is generally a relationship between a wave's (not wav) frequency and wavelength, but none that I know of regarding amplitude.

Your SampleRate is similar to a frequency, being measured in Hz, but it only tells you about the precision of the recording, rather than the actual frequency of the sounds recorded.

grossvogel
Sounds right to me.
e.James
Cheers for the info!
monkeyguy
+3  A: 

In almost all cases, an audio file has no single frequency. A sound in which the sound wave has a single frequency, is (typically) a pure sine tone, and sounds like this:

http://www.wolframalpha.com/input/?i=sound+440+Hz&a=*MC.~-_*PlaySoundTone-&a=*FS-_**DopplerShift.fo-.*DopplerShift.vs-.*DopplerShift.c--&f3=10+m/s&f=DopplerShift.vs_10+m/s&f4=340.3+m/s&f=DopplerShift.c_340.3+m/s&a=*FVarOpt.1-_***DopplerShift.fo-.*DopplerShift.fs--.***DopplerShift.DopplerRatio---.*--&a=*FVarOpt.2-_**-.***DopplerShift.vo--.**DopplerShift.vw---.**DopplerShift.fo-.*DopplerShift.fs---

This is a pure 440 Hz sine wave. (It was not possible to make a proper link of this, due to MarkDown limitations.)

A general sound, such as a recording (of speech, music, or just urban noise), consists of (an infinite number of) combinations of such sine waves, superimposed. That is, if you were to draw the graph of pressure vs. time (at a given point in space) of the wave, or, (more or less) equivalently, the position of the speaker's membrane as a function of time, it would hence not be a pure sine wave, but something much more complicated. (Indeed, how could all the information of a Beethoven symphony be represented in a simple sine wave, that is completely determined by only its frequency, a single number?)

The sampling rate of a digital recording is merely the number of samples per second of the sound wave. Indeed, a physical sound wave has an amplitude p(t) at each time, so, because there are an infinite number of times t between 0 s and 10 s (say), theoretically, to save the audio we would need an infinite number of bytes (each sample requireing a fixed number of bytes -- for instance, a 16-bit recording utilizes 16 bits, or 2 bytes, per sample -- of course, the higher the "bit number" is, the higher quality we get; for a 16-bit sound, we have 216 = 65536 levels to choose from when specifying a single sample). In practice, a sound is sampled, so that the amplitude p(t) is saved only at fixed intervals. For instance, a typical audio CD has a sampling rate of 44.1 kHz; that is, a sample is saved every 22.7 µs.

Hence, a pure sine wave of any frequency, or any recording, could be stored on a computer using any sampling rate, the quality of the recording determined by the sampling rate (the higher the better). [Technical note: Of course there is a lower limit (in some sense) on the sampling rate. This is called the Nyquist rate.]

To determine the mean frequency of the sound at any small time, you could use some advanced techniques from Fourier analysis, but it is not entirely trivial.

Andreas Rejbrand
Thanks a lot for the input! So for my example it saves 16,000 samples a second (Which I have).I have input coming in off the microphone which is recording, I'm just not sure how to find the average Frequency (as to determine which note is currently playing that second).I'll have a look at this FFT though.
monkeyguy
@monkeyguy: I just added more details to the answer.
Andreas Rejbrand
+2  A: 

As a quick addendum to the other two answers, if you are trying to measure frequencies within the sound file itself, you will need to look into the Fast Fourier Transform (FFT), which is an algorithm used to determine the strength of the frequencies within a sampled data set.

e.James
Thanks for the info I'll check it out
monkeyguy
+4  A: 

What exactly is it that you're wanting to do? The method will depend entirely on what you're hoping to achieve. Do you have a signal that contains a single sinusoid, eg a detector from a piece of mechanical equipment? Or more likely, are you wanting to play/sing into a microphone and transcribe the music?

In both cases, the FFT will be your first port of call. In the first case this may be pretty much all you need, as FFTs are good for isolated steady-state sinusoids. In the latter case, you have got a very long road ahead of you in order to get any useful results at all. Pitch recognition is a difficult problem, and merely throwing some FFTs won't get you very far. You'll need to have a good grounding in digital signal processing and also the characteristics of musical signals, and then probably your best bet is to use an autocorrelation based method instead.

See my previous answer on a related subject for some links which may be useful: http://stackoverflow.com/questions/3141927/algorithms-for-determining-the-key-of-an-audio-sample/3142776#3142776

the_mandrill
I am trying to see if I can find the current note playing so when I strum a guitar I can find the frequency and see how far off it is,
monkeyguy
+1 for DSP, pitch recognition difficulty and "throwing some FFTs won't get you very far" -- would give more if I could!
walkytalky
@monkeyguy: so a guitar tuner basically? So in that case FFTs aren't likely to help because the resolution won't be accurate enough. So I'd look into some of the classical pitch detection methods such as autocorrelation.
the_mandrill
A: 

While it's true that an audio recording will not have a single frequency, you can find the fundamental frequency easily enough. Start at the beginning of your sample, and trace through it; you're looking for the highest absolute value, and in a wave with multiple frequencies you will not know what it is until you're back to zero. Remember the highest or lowest value you've seen thus far. Now, trace forward, hopefully in the opposite direction. You're looking for the next peak or trough of similar absolute value as the one you found, using the same method as before. Find out how many samples there are between your two highest absolute value readings. Divide your sample rate by this number (it better not be zero) and then divide by 2. This is the lowest, or fundamental, frequency of your recording at this point.

You can also generate a sinusoidal function that represents a synthetic waveform at a given frequency, and subtract this waveform's instantaneous values from your sample. Find the difference in the root-mean-square amplitudes of the before and after samples. This difference is a rough approximation of the amplitude of the signal at that frequency. Repeat this process, doubling the frequency each time. You can use this to create a basic EQ spectrum.

KeithS
Simple peak-to-peak interval is quite unlikely to give good results unless the fundamental frequency is very distinct. Measurement error in the samples is rather likely to lead to aliasing. A Fourier transform should be much more robust.
walkytalky
... If you want to do a Fourier transform. Like another poster said, it's not entirely trivial to do so.
KeithS
True, but trivial solutions to non-trivial problems tend to be unsatisfactory. And even though FFT isn't trivial, it is very well studied and widely available.
walkytalky