无线计数器.docx
《无线计数器.docx》由会员分享,可在线阅读,更多相关《无线计数器.docx(16页珍藏版)》请在冰豆网上搜索。
无线计数器
通信原理实验
无线发送计算器
实验目的:
综合应用所学知识,设计无线数字传输系统,加深对通信原理所涉及内容的理解。
实验内容:
利用声音波段作为载波,设计一个基于两台电脑和两个耳麦的无线数据传输系统。
在电脑A上输入一系列命令,对其作编码调制后,通过耳麦传送到电脑B,电脑B执行命令后将结果返回到电脑A。
实验平台:
1.两台普通个人电脑;
2.2个低质量耳麦,分别连接到一台电脑上;
3.应用软件:
Matlab
技术指标:
1.在“发送电脑”上输入一个整数四则运算式(+、-、*、/),运算式中数字为0~10000的整数。
输入完成后,这个算式通过耳麦被传输到“接收电脑”上,“接收电脑”对算式进行处理后将正确的结果同样通过耳麦返回到“发送电脑”上,“发送电脑”对此接收后显示结果。
2.在算式输入完毕后1秒钟内要将结果显示出来。
如果在传输过程中出现错误,要能自动进行重传。
重传N次,最长的等待时间就为N秒。
3.两个耳麦的距离为10cm.
4.传输时,声卡上所有的音量(录、放)都将调为最大值。
5.使用Matlab时用M文件编写,每台电脑上只允许运行一个Matlab进程。
过程概述:
先由电脑A键盘输入一个算式,如A+B,由于输入为字符串,不方便直接运算,于是将A,B转化成为对应十进制的数,再然后转化二进制数,而运算符号用两个二进制数表示,同时直接计算出结果,便于结果重传时判断接收是否正确。
调制过程用2ASK方式,将调制后的结果存入长度为52位的J矩阵,在0~19位存入1,在20位存入0,接收过程中,如果电脑B收到连续的多个高电平,之后又有一个低电平表示接收开始。
在21~34存入第一个操作数(A);在35~48位存入其第二个操作数(B);在49~50位为运算符,最后一位用于矫正,如果没有会容易产生误差。
电脑A发送结束后,对接收到的声音信号进行解调解码,解码过程与编码过程原理相似,之后的到了算式A+B进行计算将计算结果存入result中用同样的方法发回电脑A,电脑A又将收到的声音信号解调,如果result和电脑A中记录的结果相同则正确,如果不相同则显示出错,并重新发送
程序代码:
clc;
clear;
closeall;
fc=1000;
fs=8000;
t=0:
1/fs:
0.01;
t=2*pi*fc*t;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<<<<<初始化<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
len=length(t);%初始化
data=zeros(1,30);%初始化数组前14目的操作数后14源操作数最后符号
read=1;%用于监视
dst=0;
src=0;
result=0;%%%%%%%计算结果
anss=zeros(1,28);%抽样结果,即结果的二进制形式
redo=1;%自动重发标志
b=fir1(48,fc/fs);%低通滤波器fc/fs归一化频率
mode=input('Pleaseenterthemodenumber."1"fortransmiterand"0"forreciver\n');%%%%模式选择,1:
发送端,0:
接收端
%%%%%%%%%%%%¥¥¥¥¥¥¥¥¥¥¥%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<<电脑A>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%¥¥¥¥¥¥¥¥¥¥¥%%%%%%%%%%%%%%
if(mode==1)%发送端
formula=input('Pleaseentertheformula\n','s');
tic%计时开始
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<<<<<<<<<<<<<<<<<<电脑A将算式转换成十进制数>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%forh=1:
length(formula)%将操作数和操作符储存
switchformula(h)%
case'0'
tmp=0;
case'1'
tmp=1;
case'2'
tmp=2;
case'3'
tmp=3;
case'4'
tmp=4;
case'5'
tmp=5;
case'6'
tmp=6;
case'7'
tmp=7;
case'8'
tmp=8;
case'9'
tmp=9;
case{'+','-','*','/'}%
opt=formula(h);%符号存入opt
read=0;%读出符号
tmp=0;
end
if(read)%
dst=dst*10+tmp;%转成十进制
else
src=src*10+tmp;
end
end
dst0=dst;%对操作数进行备份
src0=src;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<<<<<<<<<<<<<<<<<<<<<<<将十进制变成二进制>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%if((dst>=0)&&(dst<=10000)&&(src>=0)&&(src<=10000)&&((opt=='+')||(opt=='-')||(opt=='*')||(opt=='/')))%操作数、操作符编码
fori=1:
14
data(i)=mod(dst,2);%转成二进制
data(14+i)=mod(src,2);
dst=fix(dst/2);
src=fix(src/2);
end
if(opt=='+')%
data([2930])=[00];
result0=dst0+src0;
elseif(opt=='-')
data([2930])=[01];
result0=dst0-src0;
elseif(opt=='*')
data([2930])=[10];
result0=dst0*src0;
else
data([2930])=[11];
result0=fix(dst0/src0);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<<<<<<<<<<<<<<<<<<<<<<<<<<<2ASK调制>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%forj=0:
51
if(j<20)%发送开始指示
ask=sin(t);
elseif(j==20)%起始位
ask=0*t;
elseif(j<51)%2ASK调制
ask=data(j-20)*sin(t);
else
ask=0*t;%避免最后一位无法发送
end
output(j*len+1:
(j+1)*len)=ask;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<<<<<<输入值超出范围时>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%result=-9999999999;
%while(result~=result0)%出错自动重传
if(result~=-9999999999)
disp('Transmitederror,retransmitting...');
end
wavplay(output,fs);%发送
%start=zeros(1,8);
%while(max(abs(start))<0.1)%监视有无信号
%start=wavrecord(8,80);
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<接收电脑B返回信号电脑A解调>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%signal=wavrecord(fs,fs);%接收
signal=abs(signal);%包络检波
signal=filter(b,1,signal);
signal=signal/max(abs(signal));%%%归一化
scan=0;
si=1;
while(scan<0.5)%搜索信号1
scan=signal(si);
si=si+1;
end
while(scan>0.1)%搜索起始位0
scan=signal(si);
si=si+1;
end
si=si+fix(len/2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<<<<<<<<<<<将收到信息转换成相应二进制再转化成十进制>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%o=28;
result=0;
while(o>0)%抽样
if(signal(si+81*o)>0.4)
anss(o)=1;
else
anss(o)=0;
end
if(o<28)
result=result*2+anss(o);
end
o=o-1;
end
if(anss(28))
result=0-result;
end
%end
disp(result);
toc
else
disp('ERROR\n');
end
%%%%%%%%%%%%%%¥¥¥¥¥¥¥¥¥¥¥¥¥%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<<<<<<<<电脑B>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%¥¥¥¥¥¥¥¥¥¥¥¥¥%%%%%%%%%%%%%%
elseif(mode==0)%接收端
%while
(1)
%while(max(abs(start))<0.1)
%start=wavrecord(8,80);
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<<<<<<<<解调>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%signal=wavrecord(0.5*fs,fs);
signal=abs(signal);%包络检波
signal=filter(b,1,signal);
signal=signal/max(signal);
scan=0;
si=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<同步,将二进制转化为十进制>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%while(scan<0.5)
scan=signal(si);
si=si+1;
end
while(scan>0.1)
scan=signal(si);
si=si+1;
end
si=si+fix(len/2);
dst=0;
src=0;
l=30;
while(l>0)
if(signal(si+81*l)>0.4)
data(l)=1;
else
data(l)=0;
end
if(l<15)
dst=dst*2+data(l);
elseif(l<29)
src=src*2+data(l);
end
l=l-1;
end
if(data([2930])==[00])
result=dst+src;
elseif(data([2930])==[01])
result=dst-src;
elseif(data([2930])==[10])
result=dst*src;
elseif(data([2930])==[11])
result=fix(dst/src);
else
result=0;
end
result0=result;%备份
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<将计算结果转化为二进制>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(result<0)
anss(28)=1;
end
result=abs(result);
form=1:
27%dec2bin
anss(m)=mod(result,2);
result=fix(result/2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%<<<<<<<<<<<<<<<<<<<<连同同步信息一起调制>>>>>>>>>>>>>>>>>>>>>>>>>%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
forn=0:
49
if(n<20)
ask=sin(t);
elseif(n==20)
ask=0*t;
elseif(n<49)
ask=anss(n-20)*sin(t);
else
ask=0*t;
end
output(n*len+1:
(n+1)*len)=ask;
end
wavplay(output,fs);
disp(result0);
%end
else
disp('Wrongmode');
end