필터 지우기
필터 지우기

Help with for loop

조회 수: 2 (최근 30일)
benny
benny 2014년 11월 19일
편집: Andrei Bobrov 2014년 11월 19일
I need help or direction on for loops given the question
Use a loop to create a vector vout that contains the 5‐value moving average of vin.  Since it takes 5 values of input to create the first value in the output, there will be a slight delay in the output.  In other words, the first non‐zero value of vout will occur at the 5th position of vout and will be the average of vin(1) through vin(5).
given information
f1 = 100; % The lower frequency in cycles per second
f2 = 400; % The higher frequency in cycles per second
w1 = 2*pi*f1; % The lower frequency in radians per second
w2 = 2*pi*f2; % The higher frequency in radians per second
T1 = 1/f1; % The period of the lower frequency
T2 = 1/f2; % The period of the higher frequency
ns = 20; % Number of samples per period of high freq. component
% Plot the signal over 4 periods of the lower frequency component. The
% increment of the composite signal will be the period of the higher
% frequency component divided by ns.
t = (0:T2/ns:4*T1);
vin = cos(w1.*t) + cos(w2.*t);
vout(k) = (vin(k) + vin(k-1) + vin(k-2) + vin(k-3) + vin(k-4) ) / 5
This is what i tried
for k= 5:90;
vout(k) = (vin(k) + vin(k-1) + vin(k-2) + vin(k-3) + vin(k-4) ) / 5
end
plot(k,vin)
plot(k,vout)
  댓글 수: 3
benny
benny 2014년 11월 19일
I dont get anything on the graph, my values for vin were guessed
Guillaume
Guillaume 2014년 11월 19일
plot(vin);
plot(5:numel(vout), vout);
should solve that (assuming hold all). But again, that has nothing to do with the loop. Since your question is about the loop, again, what problem are you having with it?

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 11월 19일
편집: Andrei Bobrov 2014년 11월 19일
f1 = 100;
f2 = 400;
w1 = 2*pi*f1;
w2 = 2*pi*f2;
T1 = 1/f1;
T2 = 1/f2;
ns = 20;
t = (0:T2/ns:4*T1);
vin = cos(w1.*t) + cos(w2.*t);
vin = vin(:);
out = conv2(vin,ones(5,1)/5,'valid');
with loop
out = zeros(numel(vin)-4,1);
for jj = 1:numel(vin)-4
out(jj) = mean(vin(jj:jj+4));
end
plot:
plot([vin(5:end), out])

추가 답변 (1개)

MA
MA 2014년 11월 19일
you have common mistakes,here your correct code:
clear all
close all
clc;
f1 = 100; % The lower frequency in cycles per second
f2 = 400; % The higher frequency in cycles per second
w1 = 2*pi*f1; % The lower frequency in radians per second
w2 = 2*pi*f2; % The higher frequency in radians per second
T1 = 1/f1; % The period of the lower frequency
T2 = 1/f2; % The period of the higher frequency
ns = 20; % Number of samples per period of high freq. component
% Plot the signal over 4 periods of the lower frequency component. The
% increment of the composite signal will be the period of the higher
% frequency component divided by ns.
t = (0:T2/ns:4*T1);
vin = cos(w1.*t) + cos(w2.*t);
for k=5:90
vout(k) = (vin(k) + vin(k-1) + vin(k-2) + vin(k-3) + vin(k-4) ) / 5;
end
k=5:90;
voutn(1,:)=vout(5:90);
subplot(2,1,1)
plot(t,vin)
title('vin')
subplot(2,1,2)
plot(k,voutn)
title('vout')

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by