How to save a loop data

7 views (last 30 days)
Bahaa Algebory
Bahaa Algebory on 22 Oct 2016
Commented: Walter Roberson on 24 Oct 2016
Hi guys, I have a for loop, but every iteration overwrites the variable(max_run), and I have only the final data left. How can I save data from every loop? I saw some other questions like my issue, but I always get an error"Improper assignment with
%%BPSK Generation
clear all; close all; clc;
N = 2*10^4; % number of bits or symbols
ip = rand(1,N)>0.5; % generating 0,1 with equal probability
s = 2*ip-1; % BPSK modulation 0 -> -1; 1 -> 1
%%Prepare transmitted signal
m=2; % oversampling factor
beta=0.3; % rolloff parameter for SRRC
l=100; % 1/2 length of pulse shape (in symbols)
chan=[1]; % T/m "channel"
toffset=-0.5; % initial timing offset
pulshap=srrc(l,beta,m,toffset); % SRRC pulse shape
sup=zeros(1,N*m); % upsample the data by placing...
sup(1:m:end)=s; % ... p zeros between each data point
hh=conv(pulshap,sup); % ... and pulse shape
r1=conv(hh,chan); % ... to get received signal
%%Noise Generation
SNRdB=1:2:13; % Signal to Noise Ratio
SNR=10.^(SNRdB/10);
for cv=1:length(SNR);
no=sqrt((1/SNR(cv)))*randn(1,40400); % Noise generation
r=r1+no; % Adding the noise to the received signal
%%Matched Filter & Convolution process
matchfilt=srrc(l,beta,m,0); % filter = pulse shape
x=conv(r,matchfilt); % convolve signal with matched filter
%%Run clock recovery algorithm
tnow=2*l*m+1; tau=0; xs=zeros(1,N); % initialize variables
tausave=zeros(1,N); tausave(1)=tau; i=0;
mu=0.05; % algorithm stepsize
delta=0.1; % time for derivative
while tnow<length(x)-2*l*m % run iteration
i=i+1;
xs(i)=interpsinc(x,tnow+tau,l); % interpolated value at tnow+tau
x_deltap=interpsinc(x,tnow+tau+delta,l); % get value to the right
x_deltam=interpsinc(x,tnow+tau-delta,l); % get value to the left
dx=x_deltap-x_deltam; % calculate numerical derivative
tau=tau+mu*dx*xs(i); % alg update (energy)
tnow=tnow+m; tausave(i)=tau; % save for plotting
indexmin = find(tausave >= 0.5); % finding iteration value..
max_run = min(indexmin) % automaticlly
end
% Plot results
figure, subplot(2,1,1), plot(xs(1:i-2),'b.');
legend([ 'SNR=' int2str(SNR(cv))]); % plot constellation diagram
title('constellation diagram');
ylabel('estimated symbol values')
subplot(2,1,2), plot(tausave(1:i-2))
ylabel('offset estimates'), xlabel('iterations')
legend([ 'SNR=' int2str(SNR(cv))]);
end

Accepted Answer

Walter Roberson
Walter Roberson on 23 Oct 2016
max_run(i) = min(indexmin);
  4 Comments
Bahaa Algebory
Bahaa Algebory on 24 Oct 2016
Edited: Walter Roberson on 24 Oct 2016
Walter, thank you again,
Yes, there are values for 'indexmin'. This is the code of 'srrc' function.
function s=srrc(syms, beta, P, t_off);
% s=srrc(syms, beta, P, t_off);
% Generate a Square-Root Raised Cosine Pulse
% 'syms' is 1/2 the length of srrc pulse in symbol durations
% 'beta' is the rolloff factor: beta=0 gives the sinc function
% 'P' is the oversampling factor
% 't_off' is the phase (or timing) offset
if nargin==3, t_off=0; end; % if unspecified, offset is 0
k=-syms*P+1e-8+t_off:syms*P+1e-8+t_off; % sampling indices as a multiple of T/P
if (beta==0), beta=1e-8; end; % numerical problems if beta=0
s=4*beta/sqrt(P)*(cos((1+beta)*pi*k/P)+... % calculation of srrc pulse
sin((1-beta)*pi*k/P)./(4*beta*k/P))./...
(pi*(1-16*(beta*k/P).^2));
Actually, now I'm required to find the value of offset point" Y axis" where the curve becomes stable for the same value which is about "0.5" by depending on iteration values. So, any help will be appreciated.
Walter Roberson
Walter Roberson on 24 Oct 2016
Your line
xs(i)=interpsinc(x,tnow+tau,l); % interpolated value at tnow+tau
uses an undefined routine interpsinc . I grabbed interpsinc from the File Exchange but you appear to be asking to interpolate all the x values at time tnow+tau and you have over 40000 x values so the result is over 40000 results, which do not fit into the single location xs(i)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!