6

I'm working on a project that involves looking at the changes in pitch/frequency over time with a wave file (I'm new to MATLAB, but not to programming). I'm able to see the time-amplitude graph and frequency-amplitude (after an FFT) graph, but how would I be able to isolate the frequency and show it at each point in time?

Code:

filename = '/Users/Username/Sample_1.wav'

[y, fs] = wavread(filename);
y = y(:,1);
dt = 1/fs;
t = 0:dt:(length(y)*dt)-dt;
plot(t,y); xlabel('Seconds'); ylabel('Amplitude');

transformed = fft(y);
mag = abs(transformed);
plot(mag);
airplaneman19
  • 1,139
  • 6
  • 19
  • 32
  • 2
    You say "how would I be able to isolate the frequency and show it at each point in time?" There are some good answers to this, as far as it goes, but it's important to realize that there's no such thing as instantaneous frequency in the sense that you want. In other words, there is no frequency value that uniquely corresponds to each point in time (or, at least, you can't derive that information from the time-domain data. If you could, you would be violating the Heisenberg uncertainty principle). – Bjorn Roche Jan 15 '13 at 15:11

2 Answers2

8

If you have the Signal Processing Toolbox, then you may find the spectrogram function useful.

If you don't, then you can achieve the same effect manually by calculating FFTs of consecutive (possibly overlapped) windowed segments of your time-domain data, and then plotting the amplitudes.

This is essentially the short-time Fourier transform (STFT).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • To add on.. what you (the OP) are trying to do falls within the area of Time-Frequency Analysis. If you're interested, there is a lot of articles written on using techniques like Short Term Fourier Transform (what the MATLAB `spectrogram` function uses) etc. It's a fun topic! – notthetup Jan 15 '13 at 12:54
  • Thanks for your help! I was actually looking for something more along the lines of a line graph, but I think can learn to read spectograms. – airplaneman19 Jan 16 '13 at 00:13
  • @airplaneman19: Glad I could help. But do take note of Bjorn's comment above. – Oliver Charlesworth Jan 16 '13 at 00:13
0

If you have the Signal Processing Toolbox, spectrogram is the way to go (as Oli Charlesworth mentioned).

If you don't have it, the MATLAB Central File exchange is always a good place to look for something that general.

http://www.mathworks.com/matlabcentral/fileexchange/1553-spectrogram-short-time-ft-log-magnitude

This seems to be a sensible and well working implementation of the spectrogram functionality.

Dani Gehtdichnixan
  • 1,265
  • 1
  • 11
  • 21