Code showing error how to correct the code?

7 vues (au cours des 30 derniers jours)
Suchithra K S
Suchithra K S le 28 Mar 2019
Réponse apportée : Anudeep Kumar le 3 Avr 2025 à 18:16
The code given below,
clear all;
close all;
[x,fs1]=audioread('cryrumble.wav');
% ts1=1/fs1;
% N1=length(x);
% Tmax1=(N1-1)*ts1;
% fsu=fs/(N-1);
% t=(0:ts:Tmax);
% f=(-fs/2:fsu:fs/2);
% figure, subplot(411),plot(t,x),xlabel('Time'),title('Original Speech');
% subplot(412),plot(f,fftshift(abs(fft(x)))),xlabel('Freq (Hz)'),title('Frequency Spectrum');
% fs2 = (20/441)*fs1;
% na=resample(audio,2000,44100);
% N2=length(na);
ts1=1/fs1;
N1=length(x);
Tmax1=(N1-1)*ts1;
t1=(0:ts1:Tmax1);
figure;
plot(t1,x),xlabel('Time'),title('Original audio');
fs2 = (20/441)*fs1;
na=resample(x,2000,44100);
%sound(y,fs2);
ts2=1/fs2;
N2=length(na);
Tmax2=(N2-1)*ts2;
t2=(0:ts2:Tmax2);
fsu=fs2/(N2-1);
f=(-fs2/2:fsu:fs2/2);
% Step 1: Pre-Emphasis
a=[1];
b=[1 -0.95];
y=filter(b,a,na);
subplot(413),plot(t2,y),xlabel('Time'),title('Signal After High Pass Filter - Time Domain');
subplot(414),plot(f,fftshift(abs(fft(y)))),xlabel('Freq (Hz)'),title('Signal After High Pass Filter - Frequency Spectrum');
% Step 2: Frame Blocking
frameSize=882;
frame_duration=0.025;
frame_len = frame_duration*fs2;
framestep=0.01;
framestep_len=framestep*fs2;
num_frames =floor(N2/frame_len);
frames=[];
for j=1:num_frames
frame=na((j-1)*framestep_len + 1: ((j-1)*framestep_len)+frame_len);
max_val=max(frame);
if (max_val>0.025)
frames=[frames;frame'];
end
end
% Step 3: Hamming Windowing
NumFrames=size(frames,1);
hamm=hamming(frame_len)';
windowed=[];
for i=1:NumFrames
windowed(i,:)=frames(i,:).*hamm;
end
% Step 4: FFT
for i=1:NumFrames
ft(i,:)=abs(fft((windowed(i,:)),frame_len));
plot(ft(i,:))
end
% Step 5: Mel Filterbanks
Lower_Frequency =100;
Upper_Frequency = fs2/2;
Nofilters=20;
lowhigh=[100 fs2/2];
%Here logarithm is of base 'e'
lh_mel=1125*(log(1+lowhigh/700));
mel=linspace(lh_mel(1),lh_mel(2),Nofilters+2);
figure;
plot(mel);
xlabel('frequency in Hertz');ylabel('mels');
title('melscale');
melinhz=700*(exp(mel/1125)-1);
%Converting to frequency resolution
fres=floor(((frameSize)+1)*melinhz/fs2);
%Creating the filters
for m =2:length(mel)-1
for k=1:frameSize/2
if k<fres(m-1)
H(m-1,k) = 0;
elseif (k>=fres(m-1)&&k<=fres(m))
H(m-1,k)= (k-fres(m-1))/(fres(m)-fres(m-1));
elseif (k>=fres(m)&&k<=fres(m+1))
H(m-1,k)= (fres(m+1)-k)/(fres(m+1)-fres(m));
elseif k>fres(m+1)
H(m-1,k) = 0;
end
end
end
%H contains the 20 filterbanks, we now apply it to the
%processed signal.
for i=1:NumFrames
for j=1:Nofilters
bankans(i,j)=sum((ft(i,:).*H(j,:)).^2);
end
end
figure;
plot(H);
xlabel('Frequency');ylabel('Magnitude');
title('Mel-Frequency Filter bank');
% Step 6: Nautral Log and DCT
% pkg load signal
%Here logarithm is of base '10'
logged=log10(bankans);
for i=1:NumFrames
mfcc(i,:)=dct2(logged(i,:));
end
%plotting the MFCC
figure
hold on
for i=1:NumFrames
plot(mfcc(i,1:13));
end
hold off
% save c5 mfcc
i= mfcc;
save i i
The code showing the error like this,
Matrix dimensions must agree.
Error in mfccc (line 111)
bankans(i,j)=sum((ft(i,:).*H(j,:)).^2);
How to rectify this error?

Réponses (1)

Anudeep Kumar
Anudeep Kumar le 3 Avr 2025 à 18:16
Hey Suchitra,
After running your code once I found out the error you are encountering is due to a size mismatch in your element-wise multiplication. The number of columns of the variable “ft” and “H” do not match.
  • “ft(i,:)” is a 1xm vector.
  • “H(j,:)” is a 1xn vector.
Element-wise multiplication of these two vectors is not possible because they have different dimensions.
You need to ensure that the vectors you are trying to multiply have the same length. Here is something you can try:
Adjust the Size of "H": Ensure that "H" has the same number of columns as "ft" . If "H" is supposed to be a filter bank, then it should match the number of columns in "ft".
Or
Modify the Code: If "H" is meant to process segments of "ft", adjust your code to handle segments correctly.
Here's an example if you meant to apply a filter to each segment:
for i = 1:NumFrames
for j = 1:Nofilters
% Adjust this part to select the correct segment of H
% Assuming H is a filter to be applied to each row of ft
% and should have the same length as ft
if size(H, 2) == size(ft, 2)
bankans(i, j) = sum((ft(i, :) .* H(j, :)).^2);
else
error('H must have the same number of columns as ft');
end
end
end
Here is a documentation on how element-wise multiplication works in MATLAB:
I hope this helps!

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by