2010-11-10 119 views
0

你好 請嘗試生成pn序列,我有它的功能,但是當我嘗試用for循環多次重複它時,它只給我一次性答案,不會影響使用for循環爲什麼? 這是我的代碼如何用for循環重複函數?

%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 

回答

0

我假設你正在做這樣的事情:

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

和你看到的op_seq輸出每種情況下相同。這是因爲你有a,b,c作爲輸入,但你在函數的開始時覆蓋它們。如果我刪除或註釋掉在你的函數以下行:

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

然後我得到不同的結果調用不同a,b,c功能。你的函數中沒有任何隨機的,所以相同的輸入給出相同的輸出。

+0

感謝您的回覆,但是您的意思是(您指的循環必須超出您的功能範圍)。請確定代碼爲: for j = 1:50 function [op_seq] = pnseq(a,b,c) a = 7; %生成多項式x4 + x + 1 ==> b = [1 0 0 1 1 0 1] %%初始狀態[1 0 0 0] ==> c = [1 0 0 0 1 0 1] ...但它給了我錯誤,以及如何從代碼中刪除a,b,c,它不會起作用,它們是輸入和最終請清除給我(在你的函數中沒有任何隨機數,所以相同的輸入給出相同的輸出。)這句話是否意味着它不適合生成pn序列的函數 – dina 2010-11-10 02:34:46

+0

@dina看到我編輯的答案。每次調用函數之前,設置「a,b,c」是非常重要的。 「你的意思是它不適合生成pn序列的函數」:不,我只是說你需要改變輸入。相同的輸入給出相同的輸出。 – Ramashalanka 2010-11-10 02:46:43

+0

是的,我得到你的觀點非常感謝你的回答 – dina 2010-11-10 02:51:56