sir i tried one code for framing and windowing but the code showing error like this Error in lpcoefficient (line 21) wind(i,:)=​fr_ws(i,:)​.*r(i,:); how i rectify the error

the code is given below,
[x,fs]=audioread('1calf.wav');
s = x(1:93098,:);
N=length(s);
frameduration=0.00475;
framelength=frameduration*fs;
num_frames=floor(N/framelength);
count=0;
for k= 1: num_frames
frames(k,:)=s((k-1)*framelength +1 : framelength*k);
count=count +1;
end
m_amp = abs(max(frames,[],2)); % find maximum of each frame
id = find(m_amp > 0.06); % finding ID of frames with max amp > 0.03
fr_ws = frames(id,:); % frames without silence
for i=1:num_frames
v=length(fr_ws);
r=hamming(v);
wind(i,:)=fr_ws(i,:).*r(i,:);
end

2 Comments

What error? Without specifying error and without input data, how you think you will get help?
the error is like this "Index exceeds matrix dimensions.
Error in lpcoefficient (line 21)
wind(i,:)=fr_ws(i,:).*r(i,:);

Sign in to comment.

Answers (1)

One hint: In
for i=1:num_frames
v=length(fr_ws);
r=hamming(v);
wind(i,:)=fr_ws(i,:).*r(i,:);
end
v and r are constant. Then move the definition out of the loop to avoid a repeated computation. Maybe wind has the wrong size, because it is not pre-allocated - postinmg the error message would reveal this detail, so share this important message with the readers. Then:
v = length(fr_ws);
r = hamming(v);
wind = zeros(num_frames, v);
for i = 1:num_frames
wind(i,:) = fr_ws(i,:) .* r(i,:);
end
Now r(i, :) is a scalar. Is this wanted, or do you mean:
for i = 1:num_frames
wind(i,:) = fr_ws(i,:) .* r.';
end
I guess, you can omit the loop completely using the modern auto-expanding:
wind = fr_ws .* r.';

7 Comments

I tried this. BUt now also it showing the error
Matrix dimensions must agree.
Error in lpcoefficient (line 23)
wind(i,:) = fr_ws(i,:) .* r.';
You tried exactly what? Please post the code, because otherwise the readers do not know, which code produces this error.
You can e.g. type this in the command window:
dbstop if error
Afterwards run the code again until Matlab stops at the error. Then check the sizes of the arrays:
size(wind)
size(fr_ws)
size(r)
This shows the sizes of the arrays. You should be able to draw the conclusions how the code must be modified to compute, what you want.
Please note, that you did not mention, what you want to calculate. There are no comments, which explain this for each line. So if I see the failing code only, I cannot guess reliably, how it must be fixed.
I assume, that the code contains more problems, e.g.:
id = find(m_amp > 0.06);
fr_ws = frames(id,:); % Now fr_ws contains less than num_frames rows
for i = 1:num_frames
wind(i,:) = fr_ws(i,:); % But i can exceed the 1st dim of fr_ws?!
end
Actually I tried a matlab code for LPC coefficient for an audio. I want 20 lpc coeffiecients for 209 frames. ie the matrix 209x20. the code is given below,
[x,fs]=audioread('1calf.wav');
s = x(1:93098,:);
N=length(s);
frameduration=0.00475;
framelength=frameduration*fs;
num_frames=floor(N/framelength);
count=0;
for k= 1: num_frames
frames(k,:)=s((k-1)*framelength +1 : framelength*k);
count=count +1;
end
m_amp = abs(max(frames,[],2)); % find maximum of each frame
id = find(m_amp > 0.06); % finding ID of frames with max amp > 0.03
fr_ws = frames(id,:); % frames without silence
v=length(fr_ws);
r=hamming(v);
% for i = 1:num_frames
wind = fr_ws .* r.';
spectral=20*log10(abs(fft(wind)));
len=length(spectral);
ncoeff=19;
a=lpc(wind,ncoeff);
[h,f]=freqz(1,a,512,fs);
subplot(3,1,1);
plot(time,y); title(y); axis tight
%ylim=get(gca, 'ylim');
%line([time(startIndex), time(startIndex)], ylim, 'color', 'r');
%line([time(endIndex), time(endIndex)], ylim, 'color', 'r');
axis xy
% xlabel('s');
% ylabel('
subplot(3,1,2);
xj=(1:len/2)*fs/len;
plot(xj,spectral(1:len/2));
axis xy
% xlabel('frequency/Hz');
% ylabel('magnitude/dB');
subplot(3,1,3);
plot(f,20*log10(abs(h)+eps));
legend('LPC');
% xlabel('frequency(Hz)');
% ylabel(magnitude(dB)');
r=roots(a);
r=r(imag(r)>0.001);
ffreq=sort(atan2(imag(r),real(r))*fs/(2*pi));
ff=ffreq';
for i=1:length(ffreq)
fprintf('Formant %dFrequency%. 1f\n',i,ffreq(i));
end
The error is showing like this:
Matrix dimensions must agree.
Error in lpcoefficient (line 23)
wind = fr_ws .* r.';
How to rectify the error. Can you please help me to complete the code.
@Suchithra K S: Please consider my suggestion to use the debugger. This let you find out easily which sizes fr_ws and r have. I cannot run your code due to the missing input data.
Which Matlab version are you using? I've mentioned that my suggested code uses auto-expanding, so it runs in R2016b and higher.
This can be simplified also:
count=0;
for k= 1: num_frames
frames(k,:) = s((k-1)*framelength +1 : framelength*k);
count=count +1;
end
This is a simple reshape command only and count is not used aynwhere else. But even if it is needed later, your can determine the result easily without a loop: count = num_frames.
@Suchithra K S: As soon as you use the debugger and post, what
size(wind)
size(fr_ws)
size(r)
replies, I could post a working solution. Maybe you need:
% R2016a - no auto-expanding:
wind = bsxfun(@times, fr_ws, r.');
I tried the above to my code at that time also it showing error
Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.
Error in lpcoefficient (line 24)
wind = bsxfun(@times, fr_ws, r.');
How to rectify this error?

Sign in to comment.

Asked:

on 9 May 2019

Commented:

on 14 May 2019

Community Treasure Hunt

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

Start Hunting!