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!
