Plotting a variable in a for loop
Mostrar comentarios más antiguos
I am struggling to plot the error in the second for loop.
X = audiorecorder(8000,16,1);
disp('Start speaking.');
recordblocking(X,5);
disp('End of Recording.');
M = 10; % M: Order of LPC model
seg = 30; % Segment or frame size is 240
if (size(x,2) == 1) % Check if x is a column vector
x = x'; % If x is a column vector, convert it to a row vector
end
npts = length(x); % Find the number of samples
nseg = floor(npts/seg); % nseg: Number of segments in the signal
for each = 1:nseg
xx = x((each - 1)*seg + [1:seg]); % pick 240 samples for each segment
[a,G] = lpc(xx, M); % Compute lpc and variance
e = filter(a,1,xx); % Compute residual error signal
G = sqrt(G); % Calculate the standard deviation (gain) of the segment
e = e/G; % Normalize the error signal
parameter(each,:) = a;
gain(each) = G;
error((each - 1)*seg + [1:seg]) = e;
end
Respuestas (1)
I only see one for loop.
X is your audiorecorder object, and x is apparently the recorded audio data, but the relation between X and x is not given in the code, so here I'll create random data x, process it using your code and plot error:
% X = audiorecorder(8000,16,1);
%
% disp('Start speaking.');
% recordblocking(X,5);
% disp('End of Recording.');
x = randn(40000,1); % random data
M = 10; % M: Order of LPC model
seg = 30; % Segment or frame size is 240
if (size(x,2) == 1) % Check if x is a column vector
x = x'; % If x is a column vector, convert it to a row vector
end
npts = length(x); % Find the number of samples
nseg = floor(npts/seg); % nseg: Number of segments in the signal
for each = 1:nseg
xx = x((each - 1)*seg + [1:seg]); % pick 240 samples for each segment
[a,G] = lpc(xx, M); % Compute lpc and variance
e = filter(a,1,xx); % Compute residual error signal
G = sqrt(G); % Calculate the standard deviation (gain) of the segment
e = e/G; % Normalize the error signal
parameter(each,:) = a;
gain(each) = G;
error((each - 1)*seg + [1:seg]) = e;
end
plot(error)
2 comentarios
Daniela Trevino
el 24 de Abr. de 2022
Voss
el 24 de Abr. de 2022
You're welcome! If anything is not clear, let me know. Otherwise, if that solves the problem, please mark my answer as Accepted by clicking 'Accept this Answer'. Thanks!
Categorías
Más información sobre Linear Prediction en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
