Guys how can i change the vector length in column number 50 (figure 2) of my code below? i have this error "Error using plot. Vectors must be the same length"

2 views (last 30 days)
clc
%% Block Adaptation using window over a signal %%
rootdirectory = 'E:\';
[input,fs] = audioread('bsp1.wav'); % Read audio file
t = (1:length(input))/fs;
width = 300; % the width of the window
shift = width/2; % the shift of the Window
Lp = 2; % LP
window = hamming(width); % Hamming window
L = length(input); % size of input vector
Window_step_no = round((L-width)/shift); % Number of steps based on the window shift times throught the input full length
%%Reconstruct the input signal based on the framming for Lp=2
count = 1;
for i=1:Window_step_no
y(i,:) = input(count:count+width-1).*window; % multiply the signal by the hamming window and save the row values for i steps
count = count+shift; % window interval shift
a = lpc(y(i,:),Lp); % calculate the linear prediction Coeficient for each step
est(i,:) = filter([0 -a(2:end)],1,y(i,:)); % reconstruct or predict the signal for each step
err(i,:) = y(i,:)-est(i,:); % Error signal for each step or window
end
% Reshape the prediction signal by using vercat
for i=1:Window_step_no
s(i,:) = est(i,1:shift); % make a new matrix for the predicted signal
end
%Modifiying the step size to an even for the vercat function
chk = mod(Window_step_no,2);
if chk == 0
stepn = Window_step_no; % if even
else
stepn = Window_step_no-1; % if odd
count = 1;
end
for i=1:2:(stepn)
r(count:count+width-1,1) = vertcat(s(i,:).',s(i+1,:).'); % applying vercat to reshape the predicted signal
count = count+width;
end
figure(1)
plot(t,input)
xlabel('t in s')
ylabel('Signal amplitude')
title('Original Signal')
audiowrite('E:\in.wav',input,fs); % Write audio file to .wav file
figure(2)
plot(t, r)
xlabel('t in s')
ylabel('Signal amplitude')
title('Reconstructed signal with window method')
audiowrite('E:\r.wav',r,fs); % Write audio file to .wav file
errorsignal=input-r;
figure(3)
plot(t,errorsignal)
xlabel('t in s')
ylabel('Signal amplitude')
title('Reconstructed signal with window method')
audiowrite('E:\errorsignall.wav',errorsignal,fs); % Write audio file to .wav file
%% Quantize error signal
LPC_Coeff=lpc(input,Lp);
E_6 = quant(errorsignal,(2^(-6+1))); % Quantize error for 6 bit
E_7 = quant(errorsignal,(2^(-7+1))); % Quantize error for 7 bit
E_8 = quant(errorsignal,(2^(-8+1))); % Quantize error for 8 bit
x_recon6=filter([0 -LPC_Coeff(2:end)],1,E_6); %Reconstructed signal from 6bit quantized error signal
figure(4)
plot(t,x_recon6,'b');
xlabel('t in s')
ylabel('Signal amplitude')
title('Reconstructed signal from 6bit quantized error signal')
audiowrite('E:\Reconstructed_6bitnew.wav',x_recon6,fs);
x_recon7 = filter([0 -LPC_Coeff(2:end)],1, E_7); %Reconstructed signal from 7bit quantized error signal
figure(5)
plot(t,x_recon7,'b');
xlabel('t in s')
ylabel('Signal amplitude')
title('Reconstructed signal from 7bit quantized error signal')
audiowrite('E:\Reconstructed_7bitnew.wav',x_recon7,fs);
x_recon8=filter([0 -LPC_Coeff(2:end)],1,E_8); %Reconstructed signal from 8bit quantized error signal
figure(6)
plot(t,x_recon8,'b');
xlabel('t in s')
ylabel('Signal amplitude')
title('Reconstructed signal from 8bit quantized error signal')
audiowrite('E:\Reconstructed_8bitnew.wav',x_recon8,fs);
%% Variance of the original signal and error signal
varx = var(input);
vare = var(errorsignal);
m = 2;
for n=4:4:20
count = 1;
for i=1:Window_step_no
y(i,:) = input(count:count+width-1).*window;
count = count+shift;
a = lpc(y(i,:),n); % Linear prediction for different linear predictor values n
est(i,:) = filter([0 -a(2:end)],1,y(i,:));
err(i,:) = y(i,:)-est(i,:);
end
for i=1:Window_step_no
s(i,:) = est(i,1:shift);
end
count = 1;
for i=1:2:(stepn)
r(count:count+width-1,1) = vertcat(s(i,:).',s(i+1,:).');
count = count+width;
end
errorsignal1=input-r;
varen = var(errorsignal1); %error signal variance for each n value
vare = [vare varen];
m = [m n];
end
%% Prediction gain for all LP Predictors
for i=1:6
G(i)=10*(log(((varx)/(vare(i)))));
end
figure(7)
stem(m,G)
title('The new Prediction gain using Framming technique')
xlabel('No. predictors')
ylabel('prediction gain ')
G1=[ 39.5490 40.5707 43.1145 43.4124 44.0755 44.1995];
figure(8);
subplot(2,1,1)
stem(m,G1);
xlabel('No. Predictors')
ylabel('Prediction Gain')
title('Prediction gain for all LP Predictors')
hold on
subplot(2,1,2)
xlabel('No. predictors')
stem(m,G);
ylabel('prediction gain ')
title('The new Prediction gain using Framming technique')

Answers (1)

Jon
Jon on 1 Jun 2022
I may have missed something, but it looks like the first place that your variable r is assigned is the line
r(count:count+width-1,1) = vertcat(s(i,:).',s(i+1,:).'); % applying vercat to reshape the predicted signal
If that is the case then, assuming at this point count is greater than 1, your variable r(1), r(2), ... r(count-1) , will all have values of zero, and perhaps the length of r will also not be what you expect and is leading to problems with the lengths not matching in your plot 2. Is this what you intended? It is generally better to preallocate arrays (assign them to a vector, or matrix of zeros of the appropriate size) before entering a loop.
To further understand what your code is doing you will need to provide a self contained example that demonstrates the problem or attach any needed data files, e.g. bsp.wav
Also some MATLAB answers pointers for future reference. Please put a short title which generally captures the nature of your problem and then put the details in the body of the question. Also please use the code button to include code and have it nicely formatted and easy to copy.
  1 Comment
Jon
Jon on 1 Jun 2022
Stepping through with the debug tool (I recommend learning how to use this if you don't already know) I find that r has 235500 elements and t has 235736. So the two vectors definitely are not the same size.
What size would you expect r to be?
You will have to look into the details of your calculations and assignments to see why r does not have the expected number of elements.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!