Plotting a variable in a for loop
显示 更早的评论
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
回答(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 个评论
Daniela Trevino
2022-4-24
Voss
2022-4-24
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!
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Prediction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
