anyone pls give me a detailed description of what this code is doing ??? thxx

t=0 : 0.1: 30; x=10*sin (2*pi.*t/20);
figure;plot(t,x,'k','linewidth',2); xlabel('time,s'); ylabel('amplitude');
n=randn(size(t)); xn=x+n;
hold on; plot (t,xn);
wn=-5 : 1 : 5;
xc=xn;
for i = 6 : 1 : length(t) - 5
xc(i) = mean(xn(i + wn));
end
hold on; plot(t,xc,'q','linewidth',2);

댓글 수: 2

Run one line at a time. See what it does. And look at the documentation, e.g., "doc randn".
And format the code with teh code button for improved readability of your post.

댓글을 달려면 로그인하십시오.

 채택된 답변

As Andrew suggested, use doc 'functioname' to understand with more details:
Generate a row vector with numbers going from 0 to 30 with 0.1 intervals
t = 0 : 0.1: 30;
Multiply each number in t by a tenth of pi (3.1415...), take the sin of the result an multiply again by 10.
y = 10*sin(t * pi/10); % same as x = 10*sin (2*pi.*t/20);
Plot values with black line ('k') adding labels
plot(t,y,'k','linewidth',2);
xlabel('time,s');
ylabel('amplitude');
Generate a vector the same size as t with numbers distributed as a standard normal (white noise)
n = randn(size(t));
Add noise to y
yn = y+n;
Plot on the same axes
hold on;
plot (t,yn);
Generate moving average with windowsize of 11. Very weird way to do it and in my opinion WRONG at the edges Generate wn (window positions)
wn = -5:5; % same as -5:1:5; default interval is always 1
Preallocate yc by copying yn
yc = yn;
Generate running average with windowsize of 11: for each number from 6 to the length of t minus 5 elements repeat the loop incrementing the counter "i" by 1 each time.
for i = 6:length(t) - 5 % same as 6:1:length(t) - 5
% Take wn and add i.
% The first run would be [-5 -4 -3 -2 -1 0 1 2 3 4 5] + 1 = 1:11!
% So take values from yn which are positioned from 1:11 and average them
% storing them in position i of yc
yc(i) = mean(yn(i + wn));
end
More elegant way to generate moving average
wn = 11;
ycc = filter(ones(1,wn)/wn,1,yn);
Add running average to plot
% hold on; no need, just call it once and call it off with hold off
plot(t,yc,'r','linewidth',2); % 'q' as colour doesn't exist replaced with red
plot(t,ycc,'g','linewidth',2); % you can also cutoff the initial 10 values of the moving average

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2011년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by