这几天在鼓捣matlab,找了个如下的程序,准备尝试下:
%m伪随机序列Matlab源代码
%5阶m序列
% 在MATLAB命令窗口输入以下:
% fbconnection=[0 1 0 0 1];
% mseq="m"_sequence(fbconnection);
% mseq
function mseq="m"_sequence(fbconnection)
n=length(fbconnection);
N=2^n-1;
register=[zeros(1,n-1) 1]; %移位寄存器的初始状态
mseq(1)=register(n); %m序列的第一个输出码元
for i="2:N"
newregister(1)=mod(sum(fbconnection.*register),2);
for j="2:n",
newregister(j)=register(j-1);
end;
register="newregister";
mseq(i)=register(n);
end
打开matlab,将上面的代码保存为m_sequence_test.m;
然后在command中依次输入:
>> fbconnection=[0 1 0 1 0];
>> mseq="m"_sequence(fbconnection);
??? Undefined function or method 'm_sequence' for input arguments of type 'double'.
到这个指令时报错了,然后把报错信息到网上找了下,有很多,但是没有一个一样的,折腾了半天也不知道怎么回事儿。
后来又输入:
>> mseq
??? Undefined function or variable 'mseq'.
>> fbconnection="9";
>> mseq="m"_sequence(fbconnection)
??? Undefined function or method 'm_sequence' for input arguments of type 'double'.
都不行!
后来在网上找到篇文章,这样写到:
"
function [mseq]= m_sequence(fbconnection);
n = length(fbconnection);
N = 2^n-1;
register = [zeros(1,n - 1) 1];%定义移位寄存器的初始状态
mseq(1)= register(n);
for i = 2:N
newregister(1)= mod(sum(fbconnection.*register),2);
for j = 2:n,
newregister(j)= register(j-1);
end;
register = newregister;
mseq(i) = register(n);
end
"
然后就按这篇文章指示保存,运行,结果
>> mseq
mseq =
Columns 1 through 18
1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 0 0 0
Columns 19 through 31
1 1 1 1 1 0 0 1 1 0 1 0 0
OK了!!!
仔细阅读这篇文章,读到“其中自编函数m sequence. m 用来产生m 序列”时,灵感来了,这个可能跟FPGA开发一样,模块名和文件名要一致???
然后又跑回去修改我原来保存的文件名“m_sequence_test.m”为"m_sequence.m",再依次在command中输入:
>> fbconnection=[0 1 0 0 1];
>> mseq="m"_sequence(fbconnection);
>> mseq
mseq =
Columns 1 through 18
1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 0 0 0
Columns 19 through 31
1 1 1 1 1 0 0 1 1 0 1 0 0
o(∩_∩)o...哈哈!
OK了!!!
再来一个:
>> fbconnection=[1 1 0 0 0 0 0];
>> mseq="m"_sequence(fbconnection);
>> mseq
mseq =
Columns 1 through 18
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 19 through 36
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 37 through 54
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 55 through 72
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 73 through 90
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 91 through 108
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 109 through 126
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Column 127
0
7阶序列!
OK!
得到的规则:matlab中要求保存的文件名和函数名一致!
看来不看看Matlab基本的东西还是不行啊!
不过好在东西做多了发现很多东西都有触类旁通的时候!
文章评论(0条评论)
登录后参与讨论