MATLAB - Plot time-frequency graph of .wav file
Asked Answered
B

2

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);
Byelection answered 15/1, 2013 at 0:0 Comment(1)
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).Differentiation
W
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).

Whitewood answered 15/1, 2013 at 0:3 Comment(3)
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!Titular
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.Byelection
@airplaneman19: Glad I could help. But do take note of Bjorn's comment above.Whitewood
W
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.

Weathertight answered 15/1, 2013 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.