DSPRelated.com
Forums

Finding the main frequency of an input audio signal

Started by Unknown August 29, 2007
Hello,

Here's a matlab / octave example that picks the peak from the spectrum.
Read the disclaimers from previous posts. 

It will work just fine in an arts project or whatever. 
But please don't use it in elevators, pacemakers or thermonuclear
missiles.

A "test signal" (C3):
http://www.elisanet.fi/mnentwig/note.wav

Below the program. It also plots the spectrum.

Cheers

Markus


% read wave file
close all; clear all;
[y, fs, bits]=wavread('note.wav');
[n, nChan]=size(y);

% calculate the frequency corresponding to each FFT bin.
% this includes negative frequencies!
freqbase=fs*(mod(((0:n-1)+floor(n/2)), n)-floor(n/2))/n;

% apply window and get FFT. "Hamming" could be omitted
spectrum=fft(y .* hamming(n)); 

% get power (square of bins)
spectrum=spectrum .* conj(spectrum);

% pick out the audible range (for plotting)
bin1=min(find(freqbase > 16));
bin2=min(find(freqbase > 16000)-1);
spectrum=spectrum(bin1:bin2);
freqbase=freqbase(bin1:bin2);

% plot
plot(freqbase, 10*log10(spectrum+1e-15)); xlabel('f/Hz'); ylabel('p/dB');

% peak search
peak=find(spectrum==max(spectrum));
fPeak=freqbase(peak)

% the example note is a keyhole C => 9 semitones below 440 Hz "A"
fActual=440*power(2, -9/12)

@Markus
Darn... I forgot to mention that I am actually working on the
application written in Java (but even similar things would be good -
e.g. C, C++, C#). I am sorry about that. What I can hope is that this
will help somebody else looking for the solution in Matlab.

>But please don't use it in elevators, pacemakers or thermonuclear
missiles. < Darn... :))))) Just kidding, this is, as you said, "It will work just fine in an arts project or whatever. "-type of thing. @Rune
> Then read a book on the subject, contemplate what you read,
and become less confused. < You think the book is the only solution? Is there something smaller for the start? Look at the end of the post for the reasons why. Look at my previous post for at least one solution that is not a book.
> Sure. And I would just love to find that pile of gold everybody
knows is where the ranbow ends. < Oh, come on! As if it never happened to you that you did something that was hard much easier then you thought (and it worked). I am not searching for a gold fish granting me three wishes (although one would be good :). As the matter of fact, you should have read my previous post before writing your own. I already found the solution and gave a link to it. It's quite simple (one web page), took me 2 hours to swallow (together with researching other things), I am implementing it now and it is working as a charm for what I need.
> Maybe you don't want that, but that's a brilliant
way to start, if you want to learn something. < Maybe. Read my previous post to see what I want.
> You are really something, aren't you? How much maths
can you learn in two days? < Nope, unfortunately... :( That is why I would like to avoid the maths as much as possible. Since everybody else is telling me this is not a good way, maybe it definitely is not. Although, I had one very big thing behind myself that I also asked on Google groups (not comp.dsp) some time ago and everyone was telling me it's just not going to work as I proposed, but it actually worked much better then I imagined. I am so happy I did it as I thought and it worked. It wasn't pure luck it worked - it's a huge system, luck just doesn't happen with these.
> I might have one or two suggestions, but they would require
some actual *work* on your part. reading books, working through the mats, playing with code examples. You have already excluded all of that from your list of acceptable activities, so mentioning such options is a mere waiste of time. Rune < I agree with all you said. I seem to be completely misunderstood on the matter. I am not excluding all that, I am just searching for the easiest solution. Propose me a few solutions - I would choose the one that I think is better. If it takes a year to do it, I just cannot invest that much into this - is this so hard to understand?. I am OK with doing all the stuff you mentioned, but if you understand me - I rather wouldn't. If it's a must, then... Is this clearer right now? Why are you people so pessimistic? Instead of telling somebody what to do, you are telling some sarcastic philosophy stories. Maybe I wanted too much, maybe I didn't explain myself well, maybe this, maybe that, but - why do you think everything somebody else does is not working hard enough, not doing it right, not smart enough, not this, not that? If you think I am wrong, good - tell me as you did. However, your post is equally worthless if you don't tell me how to do it better. You told me - read a book. Yes, I am so dumb I couldn't have figured that by myself? Thanks. You always think there are no other reasons for somebody not doing something than what is directly obvious to you. You told me - work. As if I don't know that is a necessity? Anyway, this is also a rant by me - just had to :) Key: if you want to help, help. Not only me - this is a public group, many people will find this useful. Help, but in the right way. If you see somebody asking a question, somebody who doesn't know how to do something, tell him how to do it and/or how to do it better. Don't tell him he doesn't know how to do it - he wouldn't have asked the question if he knew the answer.