Error using .* Matrix dimensions must agree.
3 views (last 30 days)
Show older comments
Hemant Nagvanshi
on 2 Jun 2014
Answered: Henric Rydén
on 2 Jun 2014
I am trying to plot spectrogram of audio signal usinf fft using the followinf codes
astft.m
function [ X ] = astft( x,wl )
disp('Overlapping of window is 50%');
disp('1 Rectangular Window, 2 Hamming Window, 3 Hanning Window');
window=input('Enter your choice - ');
L=length(x);
if L<wl
z=wl-L;
x=[x,zeros(1,z)];
end
switch window
case 1
win=ones(1,wl);
case 2
win=hamming(wl)';
case 3
win=hanning(wl)';
otherwise
win=ones(1,wl);
disp('Not a right option, By default rectangular window is taken.');
end
L=length(x);
hop=ceil(wl/2);
if hop<1
hop=wl;
end
i=1; str=1; len=wl; X=[];
while(len<=L || i<2)
if i==1
if len>L
z=len-L;
x=[x,zeros(1,z)];
i=i+1;
end
x1=x(str:len);
*X=[X;fft(x1.*win)];*
str=str+hop; len=str+wl-1;
end
end
figure,subplot(2,1,1)
imagesc(abs(X));
subplot(2,1,2)
surf(abs(X));
end
-------------------------------------------
Example.m
clear, clc, close all
% load a .wav file
[x, fs] = audioread('record.wav'); % get the samples of the .wav file
x = x(:, 1); % get the first channel
xmax = max(abs(x)); % find the maximum abs value
x = x/xmax;
wl=256;
[Y]=astft(x,wl);
Matrix dimension are same then also it is showing error.. 'record.wav' is a short duration audio and length(x)=25600
1 Comment
Accepted Answer
Henric Rydén
on 2 Jun 2014
Transpose win
X=[X ; fft(x1.*win')]
and append x correctly:
x=[x ; zeros(z,1)];
0 Comments
More Answers (0)
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!