# applying for loop [closed]

I want to apply for loop. Below is my code. I have applied the for loop which is wrong. Here Length(t) is sample numbers. Here I want to create 'for' loop for a sample number 5 in particular. so how to change h in the for loop. Moreover, 'phi_sync' shoud be sample number of 5 and 'phi_acc' shoud be summation from 0 to 5 sample numbers.

 fs = 200;
ts = 1/fs;
t = 0:ts:1-ts;
fc = 50;
S = cos(2*pi*fc*t);                    %%% equation 7.1 noisless
sigma = 1.8;
phi_sync =  sigma * randn(1,length(t));   %%%% random variable with gaussian distributed
phi_acc = sigma * randn(1,length(t));   %%%% random variable with gaussian distributed
% h = 0;

for i=1:length(S)
h = phi_sync + phi_acc(i);
end


thanks for your efforts. This is actually algorithm for adding phase noise in a signal. after that i want to add h in the signal S, so i get the phase noise. However when make the new signal using S1 and try to analyze the signal in frequency domain. its showing matrix mistach.

S1 = cos(2*pi*fc*t + h);
L = length(S1);
nfft = L*100;           %%%%%% Zero padding
res = fft(S1,nfft)/nfft;     % resize into nfft nr of element % normalize the amplitude
f = fs/2*linspace(0,1,nfft/2+1);
res = res(1:nfft/2+1);
figure, plot(f,abs(res));
% figure, plot(f,angle(res));
return


## closed as unclear what you're asking by Daniel Grillo, user17592, Brian Drummond, Scott Seidman, tomnexusAug 15 '15 at 21:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

fs = 200;
ts = 1/fs;
t = 0:ts:1-ts;
fc = 50;
S = cos(2*pi*fc*t);                    %%% equation 7.1 noisless
sigma = 1.8;

% the length of S and t is 200 sample
phi_sync =  sigma * randn(1,length(t));

% to get the 5th sample, 10th sample, 15th sample and so on
phi_sync=phi_sync(1:5:end)

%%%% random variable with gaussian distributed
phi_acc = sigma * randn(1,length(t));   %%%% random variable with gaussian distributed

% new array to get the summation
phi_acc_summ=[];
for i=1:5:length(phi_acc)
summation=phi_acc(i)+phi_acc(i+1)+phi_acc(i+2)+phi_acc(i+3)+phi_acc(i+4);
phi_acc_summ=[phi_acc_summ   summation];% append the new value
end

% both phi_sync and phi_acc_summ have 40 samples which is 200/5

% finaly to get the 5th sample you have to simply add 5 in the middle
% of the for loop sine
for i=1:5:length(S)
h = phi_sync + phi_acc(i);
end


You could use an if statement to check whether the loop is in the fifth iteration i.e

for i=1:length(S)

h = phi_sync + phi_acc(i);
if i==5
end
end


And as far as accessing sample number 5 is concerned use-

phi_sync(5)

and for sum of elements use-

sum(phi_acc)

• Hey thanks Amal. I am not good with loops. How exactly do you mean? – Urban_Yogi Jul 28 '15 at 9:11
• the i variable gets assigned the value of the current iteration. Now all you need to to do is use an if statement to check whether the value of i is equal to 5, as I have given in my answer. Now '#your code' only gets executed when i is equal to 5 – Amal Vincent Jul 28 '15 at 9:16
• Forgive me if I misunderstood your question, good luck – Amal Vincent Jul 28 '15 at 9:20