开发者

how to repeat function with for loop?

I am trying to generate a pn sequence and it works. However, when I try I call the开发者_运维知识库 function with different inputs in a for-loop, it gives me the same results each time. As if it is not affected by using the for loop. Why?

This is my code:

%e.g. noof flip flops 4 ==> 
function[op_seq]=pnseq(a,b,c)
a = 7;
%generator polynomial x4+x+1    ==> 
b = [1 0 0 1 1 0 1 ]
%initial state [1 0 0 0] ==> 
c = [1 0 0 0 1 0 1 ]
%refere figure to set a relation between tap function and initial state
%
for j= 1:50,
x = a;
tap_ff =b;
int_stat= c;
for i = 1:1: length(int_stat)
    old_stat(i) = int_stat(i);
    gen_pol(i) = tap_ff(i);
end
len = (2 ^x)-1;
gen_pol(i+1)= 1;
gen_l = length(gen_pol);
old_l = length(old_stat);
for i1 = 1: 1:len
    % feed back input genration
    t = 1;
    for i2 = 1:old_l
        if gen_pol(i2)==1
            stat_str(t) = old_stat(gen_l - i2);
            i2 = i2+1;
            t = t+1;
        else
            i2 = i2+1;
        end
    end
    stat_l = length(stat_str);
    feed_ip = stat_str(1);
    for i3 = 1: stat_l-1
        feed_ip = bitxor(feed_ip,stat_str(i3 + 1)); 
        feed_ipmag(i1) = feed_ip;
        i3 = i3+1;
    end
    % shifting elements
    new_stat = feed_ip;
    for i4 = 1:1:old_l
        new_stat(i4+1) = old_stat(i4);
        old_stat(i4)= new_stat(i4);
    end
    op_seq(i1) = new_stat(old_l +1);
end
%op_seq;
end


I assume you're doing something like:

for n = 1:10
  ...
  % set a,b,c for this n
  ...
  op_seq =pnseq(a,b,c)
  ...
end

and that you see the same op_seq output for each case. This is because you have a,b,c as inputs, but you overwrite them at the start of your function. If I remove, or comment out the following lines in your function:

a = 7;
b = [1 0 0 1 1 0 1 ]
c = [1 0 0 0 1 0 1 ]

Then I get different results for calling the function with different a,b,c. There is nothing random in your function, so the same inputs give the same outputs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜