MATLAB - fft, find where it occurs in original data

Hey guys! :) For the past few hours I've been trying to find solution to my problem, but unsuccessfully. The situation is this: I have transformed audio data to fft, found a peak value and then I need to find where it occurs in original data.
My script:
LOAD THE FILE:
[AUDIO_ARRAY1 AUDIO_FS1] = wavread('AAA.WAV');
TRANSFORM IT TO FFT:
AMPLITUDE = abs(fft(AUDIO_ARRAY1));
AMPLITUDE = AMPLITUDE(1 : floor(NUMBER_OF_SAMPLES / 2)); % let's remove mirrored data
FREQUENCY = AUDIO_FS1 * (0 : floor(NUMBER_OF_SAMPLES / 2) - 1) / NUMBER_OF_SAMPLES;
FIND SOME VAL:
if max(AMPLITUDE) == 160
PEAK_VALUE = max(AMPLITUDE);
end
And now I need to find where in AUDIO_ARRAY1 my PEAK_VALUE occurs... Any ideas how to do this?

Answers (4)

You have to match the index of the maximum in the modulus of the DFT (maximum absolute value in the fft() output) with the index in a frequency vector.
Fs = 1000;
t = 0:0.001:1-0.001;
x = cos(2*pi*100*t)+randn(size(t));
xdft = fft(x);
xdft = xdft(1:length(x)/2+1);
[~,I] = max(abs(xdft));
freq = 0:Fs/length(x):Fs/2;
fprintf('Maximum occurs at %2.2f Hz.\n',freq(I));
Thank you, Wayne King, but I know how to find out at which frequency it occurs. I need to know WHEN it occurs in AUDIO_ARRAY1 (in time domain). In other words to detect when my drum sound appears in loop file (AUDIO_ARRAY1).
Let's say I found that in AMPLITUDE = abs(fft(AUDIO_ARRAY1)) vector my drum appears at index 100 (AMPLITUDE(100)) now I need to know at which sample in AUDIO_ARRAY1 it happens...
Hi Marius the DFT has no time localization because you sum over all time. Based on the output of fft(), you cannot determine when a particular frequency component occurs in the signal, only that the frequency is present.
You can use spectrogram (the short-time Fourier transform) to perform a time-frequency analysis where your temporal resolution is determined by the segment (window) length you choose. Within that window, you have again no time resolution.
Marius, you don't understand how the FFT or spectra work. The spectra is basically the weights of all the frequencies that went into making up your signal. You're probably more familiar with the Taylor series expansion where some function is made up of polynomials, so let's try to explain using that. If you have a function, say cos(x)*exp(-x) and you create a Taylor series out of it like a0 + a1*x + a2*x^2 + a3*x^3 + ..., now can you say "where" the x^3 "happened" in your original signal? No, you can't. Well a FT is just like a Taylor series except that it's a combination of sine waves instead of polynomials.
You might want to (1) look back in the time domain, using something like normalized cross correlation (normxcorr2() in the Image Processing Toolbox), or else (2) scan along your signal looking at smaller windows of time to try to identify the window location where the frequency you're looking contains a larger than "normal" value.

Products

Asked:

on 1 Apr 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!