Computing narrowband spectrograms using MATLAB [closed]

I'm having an assignment of computing narrowband spectrograms using MATLAB. And I totally have no idea about the code. Can someone help me writing the code and explaining what's going on to me. Lots of thanks!!

Followings are the requirement:

B.1. Load the recorded .wav file using the function “wavread”. Let y be the variable storing the signal;

B.2. Perform short-time Fourier transform on y as follows: (i) Divide the signal into many short-time frames. Each frame is 50 msec long. Frame shift is 1 msec;

(ii) Apply 1024-point FFT on each frame;

(iii) Store the power spectrum of each frame in a column vector (note that only the first 512 FFT coefficients are needed).

(iv) Append the power spectrum vectors of all frames to form super matrix S. The size of the matrix will be 512 × no. of frames; (v) Plot the spectrogram using the following MATLAB commands

closed as off-topic by Matt L., jojek♦, lennon310, Peter K.♦Apr 14 '14 at 19:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions requesting working code written to a specification are off-topic as they are unlikely to benefit anyone else. Instead, describe the problem you're solving and where you're stuck." – Matt L., jojek, lennon310, Peter K.
If this question can be reworded to fit the rules in the help center, please edit the question.

No one is going to give you a full working code - the point is that you should learn and understand how to do it. Following guidelines for you:

1. As given, use the wavread function to load the recording (please mind that in the newest 2014a version this function is deprecated, you should use audioread instead). MATLAB has wonderful help with very good examples, so simply use it.

2. Having your signal loaded, together with its sampling frequency, you need to split it into chunks (many short-time frames). This can be done in two ways, either you are using a loop construct (for, while - whichever you prefer), or (my favourite) use the MATLAB buffer function (please consider using 'nodelay' option in it, as first frame will have zeros in it up to overlap length). Although you require the Signal Processing Toolbox license (I'm sure your University has it). This function allows you to specify the length of frame, overlapping and other things. In result you will obtain the matrix which columns are your signal frames. Having the sampling frequency from wavread, it is very easy to calculate corresponding length of your frame in samples for given number of miliseconds

3. Now you can iterate over the columns of your matrix and calculate the FFT for each of them. You might want to use the fft function to get it. In extra parameter you can specify the length of your signal (to apply zero padding eventually). So let's say that now you have FFT for first frame. Now take the magnitude of it (abs), and choose first 512 samples. Store this result into fist column of your super matrix $S$. Optional step: before calculating the FFT, I suggest you to multiply your frame by the window function, i.e. hamming window.

4. Now you have the matrix $S$, so plotting can be done by using various functions (pcolor, imagesc, and so on - pick whichever you prefer).

I suggest you to compare the final result with MATLAB built-in spectrogram function.

Good luck!

• I don't quite understand the meaning of overlapping in the function buffer.. Is it referring to the frame shift?? – yyy Apr 14 '14 at 13:42
• Thanks so much for your help!!!!:) I'm trying my best to work on it – yyy Apr 14 '14 at 13:58
• I save y as my sample by [y,Fs]=wavread('song.wav'); then split it by y=buffer(y,80,16); then I've got a matrix of 80x749; how could I separate the matrix into columns that represent each of the frames? – yyy Apr 14 '14 at 14:18
• I'v understood the frequency, but can't deal with the time, what does "with step of overlap shift" means? if my number of frame is 2946, and each frame is 50ms long. Is it correct for I to create the time vector by "time = [0:2946-1].*(0.05)"? – yyy Apr 15 '14 at 7:42
• Finally, I've finished my project, credits to jojek:) really thanks so much for your patience, I'm such an matlab idiot. – yyy Apr 15 '14 at 9:14