MATLAB摩尔斯电码
问题描述:
写出函数tokenizeSignal(signal),它取上面的信号并计算出0和1的顺序出现的次数。输出应该是一个二维数组,其中列1是多少出现,列2是哪个令牌(0或1)。我有下面的代码,直到我把它放入函数。例如MATLAB摩尔斯电码
sig =[1 1 0 0 0 0 0 1 1 0 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 0];
tsig = abs(sig);
dsig = diff([1 tsig 1]);
startIndex = find(dsig<0);
endIndex = find(dsig>0)-1;
duration = endIndex-startIndex+1;
stringIndex = (duration >= 2);
d=find(stringIndex==0);
matA=[duration;zeros(1,size(duration,2))];
matA=matA';
wsig = abs(sig);
rsig = diff([0 wsig 0]);
startIndex = find(rsig < 0);
endIndex = find(rsig > 0)-1;
duration = endIndex-startIndex+1;
abs(duration);
stringIndex = (duration >= 2);
d=find(stringIndex==0);
type=[1];
matB=[ans;ones(1,size(ans,2))];
matB=matB';
token=reshape([matA(:) matB(:)]',size(matA,1)+size(matB,1), [])
这将返回我们所需要的,但是当我们把上面的代码放到一个函数头和类型结束的结论不再返回任何东西。为什么是这样?
答
它不工作的原因是因为你靠关键字 “答”,这是从工作区访问,而不是一个函数中,指的是ABS(持续时间)
此复制你的脚本,里面功能:
function tokens = tokenizeSignal(sig)
tsig = abs(sig);
dsig = diff([1 tsig 1]);
startIndex = find(dsig<0);
endIndex = find(dsig>0)-1;
duration = endIndex-startIndex+1;
matA=[duration;zeros(1,size(duration,2))];
matA=matA';
wsig = abs(sig);
rsig = diff([0 wsig 0]);
startIndex = find(rsig < 0);
endIndex = find(rsig > 0)-1;
duration = endIndex-startIndex+1;
yourAns = abs(duration);
matB=[yourAns;ones(1,size(yourAns,2))];
matB=matB';
tokens=reshape([matA(:) matB(:)]',size(matA,1)+size(matB,1), []) ;
谢谢,这是非常有帮助的。现在我得到一个错误,因为在一些测试条件下,matA和matB是不同的大小。我应该如何重塑它们才能获得令牌? –
您可以返回一个单元格数组,如 –
tokens = {matA(:); MATB(:)}; 用这个替换函数的最后一行。 HTH,PSS –