matlab(从程序中给出的简单编辑工作)

时间:2013-03-26 18:21:35

标签: matlab

以下程序是选择性重复协议,用于传输随机生成的帧,有时在发送帧时输入错误。帧的总帧数,窗口大小和帧长度由用户动态给出。如果窗口大小为3而帧没有为7,则协议发送前三帧生成并在接收到前三帧后发送接下来的三个生成的比特,并且在接收到先前发送的帧之后发送最后剩余的第7帧。然后在完成传输后,收到确认。如果发送第2帧时出错,则仅在第2帧重发后,才会发送下一组3帧。如果帧的长度为4,则每帧将具有5位(帧的长度+一个奇偶校验位)

%while(b==1)
clc;
m=input('Enter the total no of frames:');
n=input('Enter the window size:');
t=input('Enter the length of frames:');
disp(sprintf('\n'));
while(m~=0)
if(m<n)
n=m;
end
for i=1:1:n;
d=randint(1,t);
d(t+1)=rem(sum(d),2);
a(i,1:t+1)=d;
disp(sprintf('Transmitting frame %d :',i));
disp(a(i,1:t+1));
end
p=randint(1,1,[1,n]);
q=randint(1,1,[1,t+1]);
a(p,q)=randint(1,1);
for i=1:1:n
fprintf('Received frame %d:',i);
disp(a(i,1:t+1));
end
f=zeros(1,n);
for i=1:1:n
f(i)=rem(sum(a(i,1:t+1)),2);
if(f(i)==1)
p=i;
end
end
if(f==0)
fprintf('ACKNOWLEDGEMENT RECEIVED\n');
else
fprintf('Frame%d is REJECTED\n',p);
disp('RETRANSMITTING....');
a(p,q)=~a(p,q);
for i=p
fprintf('Received frame %d:',i);
disp(a(i,1:t+1));
end
end
m=m-n;
end

该程序的输出

Enter the total no of frames:5
Enter the window size:3
Enter the length of frames:4


Transmitting frame 1 :
     1     1     0     1     1

Transmitting frame 2 :
     1     0     0     1     0

Transmitting frame 3 :
     1     1     0     1     1

Received frame 1:     1     1     0     1     1

Received frame 2:     1     0     0     1     0

Received frame 3:     1     1     1     1     1

Frame3 is REJECTED
RETRANSMITTING....
Received frame 3:     1     1     0     1     1

Transmitting frame 1 :
     0     0     1     1     0

Transmitting frame 2 :
     1     1     0     1     1

Received frame 1:     0     0     1     1     0

Received frame 2:     1     1     0     1     1

ACKNOWLEDGEMENT RECEIVED
>> 

我需要将随机生成的帧更改为动态的用户,并且不限制帧的长度(想要输入不同长度的帧)

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望用户输入每个帧,并且一帧的长度可能与下​​一帧不同。

如果是这种情况,您可以使用以下代码:

clear;
close;
clc;

m=input('Enter the total no of frames:');
n=input('Enter the window size:');
a=-ones(n,1);
max=0;
disp(sprintf('\n'));

while(m~=0)
    if(m<n)
        n=m;
    end

    for i=1:1:n;
        while(1)
            d=input('Enter a frame: ');    %To be input as, for example, [0 1 1 0]
            if((size(d,1)~=1 && size(d,2)~=1) || length(size(d))~=2)
                disp('Invalid input! Frame must be an one dimensional array. Try again.');
                continue;
            else
                break;
            end
        end
        t=length(d);
        if(t>max)
            max=t;
            a_temp=-(ones(n,t+2));
            a_temp(1:size(a,1),1:size(a,2))=a;
            a=a_temp;
        end
        d(t+1)=rem(sum(d),2);
        a(i,1:t+1)=d;
        disp(sprintf('Transmitting frame %d :',i));
        disp(a(i,1:t+1));
    end

    p=[find(a==1);find(a==0)];
    q=ceil(rand()*length(p));
    if(q==0)
        q=1;
    end
    a(q)=randint(1,1);

    for i=1:1:n
        t=find(a(i,:)==-1,1)-2;
        fprintf('Received frame %d:',i);
        disp(a(i,1:t+1));
    end

    f=zeros(1,n);
    for i=1:1:n
        t=find(a(i,:)==-1,1)-2;
        f(i)=rem(sum(a(i,1:t+1)),2);
        if(f(i)==1)
            p=i;
        end
    end

    if(f==0)
        fprintf('ACKNOWLEDGEMENT RECEIVED\n');
    else
        fprintf('Frame%d is REJECTED\n',p);
        disp('RETRANSMITTING....');
        a(q)=~a(q);
        for i=p
            t=find(a(i,:)==-1,1)-2;
            fprintf('Received frame %d:',i);
            disp(a(i,1:t+1));
        end
    end

    m=m-n;
end

修改

您需要上一个问题的d;将上一个问题中的d替换为其他内容,例如d_indices

将行j=1;添加到上一代码的末尾。

现在将此当前代码(不含clear)添加到上一代码的末尾并进行以下更改:

  1. m=input('Enter the total no of frames:');替换为m=length(d_indices)-1;
  2. d=input('Enter a frame: ');替换为
  3. j=j+1;
    if(j>length(d_indices)
        break;
    end
    d=x((d_indices(j-1)+1):d_indices(j));
    

    我还没有尝试过,但你应该好好去。