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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by