# Identify the frequency spectrum of an audio file by using matlab [closed]

I need to identify the first few harmonics of a audio file by cancelling the noise of the specific signal. So I have partitioned the signal into 1024 Hz sample blocks and then took the FFT values of each block separately and finally add all FFT values and plot the waveform. Here is my matlab code. Somebody help me with the code since i cannot add the first block fft into total fft. Get an error. Please somebody help me

clc
clear
[signal,fs] =audioread('Female_55.wav'); %Only for plotting
t=0:1/fs:(length(signal)-1)/fs;          %Only for plotting

fs0=fs/1024                        %number of frequency blocks
t1=length(signal)/fs;              %length of the waveform

s=1;    %starting value
s1=1;   %ending value
c1=1;

wavefft_tot= zeros(1025,1);

for blocks = 1:fs0

[signal1,fs1] =audioread('Female_55.wav',[s*c1 s1*1024]);
wavefft_next=abs(fft(signal1));
wavefft_prv=wavefft_next;

n=length(signal1)-1;  %plotting information
f=0:fs1/n:fs1;        %plotting information

if(s==1) && (c1==1)
c1=1;
s=1024;
s1=s1+1;
wavefft_tot= wavefft_next;
else
s1=s1+1;
c1=c1+1;
wavefft_tot=wavefft_tot+wavefft_prv;

end

end

figure(1);
plot(f,wavefft_tot);
xlabel('Frequency in Hz');
ylabel('Magnitude');
title('The Wave FFT');

• "get an error" indicates this is not a signal processing question, but a programming question. thus, this question is off-topic here, but might be on-topic on stackoverflow.com. However, there it'll be closed, too, because "get an error" is not a problem description. Quite obviously, the verbatim error is relevant to your problem; properly indent your code, too, that might already help you solve your problem yourself. – Marcus Müller Mar 14 '17 at 13:17
• What error are you getting? Note also the FT is invariant in magnitude only under a time shift but not phase, to properly combine you would want to compensate for linear phase shift in each subgroup. Otherwise if you are only concerned with magnitude, you would benefit by taking the absolute value before combining to eliminate the possible cancellations due to phase rotation. – Dan Boschen Mar 14 '17 at 13:23
• Also if it matters there will be a small error in your frequency axis I believe; the FT goes from 0 to 1 sample less than Fs, where your range is 0 to Fs (although correct number of points). If n is large the error will likely be insignificant. – Dan Boschen Mar 14 '17 at 13:32
• @DanBoschen Actually hope you get an idea about the code. just stick into for loop. simply inside the for loop first iteration it does is take first block of the signal and convert it into FFT. actually that fft value going to store it in wavefft_prv and wavefft_next. – Umesh Wickramasuriya Mar 14 '17 at 14:01
• So what is the error or what do you think is wrong with what you are doing? (I do see now that you are taking the absolute value) ,but don't see the point of assigning wavefft_prv ? – Dan Boschen Mar 14 '17 at 14:03

## 1 Answer

It looks like you are trying to recreate the functionality in spectrogram. Spectrogram will give you a short-time Fourier transform using whatever blocksize you specify.

Much of your code looks like it is re-inventing this.

• "Error using + Matrix dimensions must agree" Actually hope you get an idea about the code. just stick into for loop. simply inside the for loop first iteration it does is take first block of the signal and convert it into FFT. actually that fft value going to store it in wavefft_prv and wavefft_next. As you can see there's a IF condition. simply that condition works only for first block. Iikewise all the blocks going to calculate fft at each iterations and save it in the wavefft_tot. – Umesh Wickramasuriya Mar 14 '17 at 14:20
• Please somebody help me to write a matlab code – Umesh Wickramasuriya Mar 14 '17 at 14:21