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 件)

Voss
Voss 2022 年 4 月 24 日

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
Daniela Trevino 2022 年 4 月 24 日
Thank you!
Voss
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!

サインインしてコメントする。

カテゴリ

質問済み:

2022 年 4 月 24 日

コメント済み:

2022 年 4 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by