实验一:
clear all
[x,fs,bits]=wavread('myheart.wav');
z=sign(x); Max=max(abs(x)); x1=abs(x/Max); Q=2048*x1;
Y=zeros(length(x1),8);
for m=1:length(x)
if Q(m)>128 & Q(m)<20## Y(m,2)=1;
end
if (Q(m)>32 & Q(m)<128) || (Q(m)>512 & Q(m)<2048) Y(m,3)=1;
end
if (Q(m)>16 & Q(m)<31) || (Q(m)>64 & Q(m)<128) || (Q(m)>256 & Q(m)<512) || (Q(m)>1024 & Q(m)<2048) Y(m,4)=1;
end
if z(m)>0 Y(m,1)=1;
elseif z(m)<0 Y(m,1)=0;
end
end
N=zeros(1,length(x1));
for m=1:length(x1)
N(m)=Y(m,2)*4+Y(m,3)*2+Y(m,4)+1;
end
a=[0,16,32,64,128,256,512,1024];
b=[1,1,2,4,8,16,32,64];
for m=1:length(x1)
q=ceil(Q(m)-a(N(m))/b(N(m)));
if q==0 Y(m,(5:8))=[0,0,0,0];
else k=num2str(dec2bin(q-1,4));
Y(m,5)=str2num(k(1));
Y(m,6)=str2num(k(2));
Y(m,7)=str2num(k(3));
Y(m,8)=str2num(k(4));
end
End
实验结果:
Max =0.3906
a =
Columns 1 through 7
0 16 32 64 128 256 512
Column 8
1024
b = 1 1 2 4 8 16 32 64
bits =16
fs =22050
k =111011011
m =168873
q =476
实验二:
RGB=imread('8.jpg');
I=rgb2gray(RGB);
J=dct2(I);
imshow(log(abs(J)),[]),colormap(jet(64)),colorbar
J(abs(J)<10)=0;
K=idct2(J);
figure,imshow(I)
figure,imshow(K,[0 255])
经反变换后的图像:
2.输入DCT进行JPEG图像压缩的代码如下:
RGB=imread('8.jpg');
I=rgb2gray(RGB);
I=im2double(I);
T=dctmtx(8);
B=blkproc(I,[8 8],'P1*x*P2',T,T');
Mask=[ 1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2=blkproc(B,[8 8],' P1*x',Mask);
I2= blkproc(B2,[8,8],'P1*x*P2',T',T);
Subplot(1,2,1);
Imshow(I);title('原图像');
Subplot(1,2,2);
Imshow(I2);title('压缩图像');
3.游程编码:
image1=imread('lena.jpg');
imshow(image1);
image2=image1(:);
image2length=length(image2);
for i=1:1:image2length
if image2(i)>=127
image2(i)=255;
else
image2(i)=0;
end
end
image3=reshape(image2,512,512);
figure,imshow(image3);
X=image3(:);
x=1:1:length(X);
figure,plot(x,X(x));
j=1;
image4(1)=1;
for z=1:1:(length(X)-1)
if X(z)==X(z+1)
image4(j)=image4(j)+1;
else
data(j)=X(z);
j=j+1;
image4(j)=1;
end
end
data(j)=X(length(X));
image4length=length(image4);
y=1:1:image4length ;
figure,plot(y,image4(y));
CR=image2length/image4length;
实验三
function code = addfade(modcode,Tf,isperiod,isfade)
%功能:向传输序列modcode叠加衰落性信道的衰落参数k(t)
if(isfade==1)
if(isperiod==1)
a=31;
b=30+10*Tf;
modcode(1,a:b)=0.1*modcode(1,a:b);
end
code=modcode;
else
code=modcode;
end
function bitcoded = channelcoding(sym,G,k)
A=vec2mat(sym,k);%把向量转换成矩阵
U=A*G;
U=mod(U,2);
bitcoded=reshape(U',1,[]);
function bitdecoded = channeldecoding(recode,Etab,Smatrix,H,n,k)
%前向纠错函数,实现纠错功能
row=length(recode)/n;
E=zeros(row,n);
RM=zeros(row,n);%纠错之后的矩阵
R=vec2mat(recode,n);
S=R*H';%伴随矩阵
S=mod(S,2);
for i=1:row
for j=1:2^(n-k)
%查表纠错
if(S(i,:)==Smatrix(j,:))
E(i,:)=Etab(j,:);
RM(i,:)=R(i,:)+E(i,:);
RM(i,:)=mod(RM(i,:),2);
break;
end
end
end
bitdecoded=reshape(RM',1,[]);%转化为比特流
tic
clc
%功能:有无信道编码性能比较
M=2;
%进制
b=log2(M);
%每符号比特数
n=128*10000;%符号数
G=[1 1 1 1 0 0 0;1 0 1 0 1 0 0;0 1 1 0 0 1 0;1 1 0 0 0 0 1];%生成矩阵
H=[1 0 0 1 1 0 1;0 1 0 1 0 1 1;0 0 1 1 1 1 0];
%监督矩阵
Etab=[0 0 0 0 0 0 0;0 0 0 0 0 0 1;
0 0 0 0 0 1 0;0 0 0 0 1 0 0;
0 0 0 1 0 0 0;0 0 1 0 0 0 0;
0 1 0 0 0 0 0;1 0 0 0 0 0 0];
%错误图样
Smatrix=Etab*H';
%对应的伴随式
sym=randint(n,1,M);
sym=de2bi(sym,'left-msb');
bitcoded=channelcoding(sym,G,4);
modbit=pskmod(bitcoded,M);
%在传输序列modbit加入AWGN噪声
snr=0:0.2:15;%噪声为0到15d
L=length(snr)
%模拟信源编码
%信道编码,(7,4)码
ser=zeros(1,L);
ser2=zeros(1,L);
for k=1:L
y=awgn(modbit,10*log10(b)+snr(k),'measured');
zsym=pskdemod(y,M);
%复数解调
zbit=de2bi(zsym,'left-msb');
recode=reshape(zbit',1,[]);
Rstream=recode;
err=(Rstream~=bitcoded);
errnum=sum(err);
ser(k)=log10(errnum/length(bitcoded));
%纠错
bitdecoded=channeldecoding(Rstream,Etab,Smatrix,H,7,4);
err=(bitdecoded~=bitcoded);
errbits=sum(err);
ser2(k)=log10(errbits/(length(bitcoded)));
end
plot(snr,ser,'b-*')
hold on
plot(snr,ser2,'r-o')
grid on
legend('没有信道编码','信道编码');
xlabel('Eb/No(dB)');
ylabel('SER');
title('2PSK有无信道编码性能比较');
toc
%
clc;
clear;
close all;
n=10000;
b=randint(1,n);
f1=1;f2=2;
t=0:1/30:1-1/30;
%ASK
sa1=sin(2*pi*f1*t);
E1=sum(sa1.^2);
sa1=sa1/sqrt(E1); %unit energy
sa0=0*sa1;
%FSK
sf0=sin(2*pi*f1*t);
E=sum(sf0.^2);
sf0=sf0/sqrt(E);
sf1=sin(2*pi*f2*t);
E=sum(sf1.^2);
sf1=sf1/sqrt(E);
%PSK
sp0=-sin(2*pi*f1*t)/sqrt(E1);
sp1=sin(2*pi*f1*t)/sqrt(E1);
%调制
ask=[];psk=[];fsk=[];
for i=1:n
if b(i)==1
ask=[ask sa1];
psk=[psk sp1];
fsk=[fsk sf1];
else
ask=[ask sa0];
psk=[psk sp0];
fsk=[fsk sf0];
end
end
figure(1)
subplot(411)
stairs(0:10,[b(1:10) b(10)],'linewidth',1.5)
axis([0 10 -0.5 1.5])
title('Message Bits');grid on
subplot(412)
tb=0:1/30:10-1/30;
plot(tb, ask(1:10*30),'b','linewidth',1.5)
title('ASK Modulation');grid on
subplot(413)
plot(tb, fsk(1:10*30),'r','linewidth',1.5)
title('FSK Modulation');grid on
subplot(414)
plot(tb, psk(1:10*30),'k','linewidth',1.5)
title('PSK Modulation');grid on
xlabel('Time');ylabel('Amplitude')
%AWGN
for snr=0:30
askn=awgn(ask,snr);
pskn=awgn(psk,snr);
fskn=awgn(fsk,snr);
%DETECTION
A=[];F=[];P=[];
for i=1:n
%ASK Detection
if sum(sa1.*askn(1+30*(i-1):30*i))>0.5
A=[A 1];
else
A=[A 0];
end
%FSK Detection
if sum(sf1.*fskn(1+30*(i-1):30*i))>0.5
F=[F 1];
else
F=[F 0];
end
%PSK Detection
if sum(sp1.*pskn(1+30*(i-1):30*i))>0
P=[P 1];
else
P=[P 0];
end
end
%BER
errA=0;errF=0; errP=0;
for i=1:n
if A(i)==b(i)
errA=errA;
else
errA=errA+1;
end
if F(i)==b(i)
errF=errF;
else
errF=errF+1;
end
if P(i)==b(i)
errP=errP;
else
errP=errP+1;
end
end
BER_A(snr+1)=errA/n;
BER_F(snr+1)=errF/n;
BER_P(snr+1)=errP/n;
end
figure(2)
subplot(411)
stairs(0:10,[b(1:10) b(10)],'linewidth',1.5)
axis([0 10 -0.5 1.5]);grid on
title('Received signal after AWGN Channel')
subplot(412)
tb=0:1/30:10-1/30;
plot(tb, askn(1:10*30),'b','linewidth',1.5)
title('Received ASK signal');grid on
subplot(413)
plot(tb, fskn(1:10*30),'r','linewidth',1.5)
title('Received FSK signal');grid on
subplot(414)
plot(tb, pskn(1:10*30),'k','linewidth',1.5)
title('Received PSK signal');grid on
figure(3)
semilogy(0:30,BER_A, 'b','linewidth',2)
title('BER Vs SNR')
grid on;
hold on
semilogy(0:30,BER_F,'r','linewidth',2)
semilogy(0:30,BER_P, 'k','linewidth',2)
xlabel('Eo/No(dB)')
ylabel('BER')
hold off
legend('ASK','FSK','PSK');
实验四
clc
clear
%Generationofbitpattern
s=round(rand(1,25));
signal=[];
%Generating20bits
carrier=[];
t=[0:2*pi/119:2*pi];
for k=1:25
%Creating60samplesforonecosine
if s(1,k)==0
sig=-ones(1,120);
%120minusonesforbit0
%120onesforbit1
else
sig=ones(1,120);
end
c=cos(t);
carrier=[carrier c];
signal=[signal sig];
end
subplot(4,1,1);
plot(signal);
axis([-100 3100 -1.5 1.5]);
title('\bf\it Original Bit Sequence');
%BPSKModulationofthesignal
bpsk_sig=signal.*carrier;
subplot(4,1,2);
%Modulatingthesignal
plot(bpsk_sig)
axis([-100 3100 -1.5 1.5]);
title('\bf\it BPSK Modulated Signal');
%Preparationof6newcarrierfrequencies
t1=[0:2*pi/9:2*pi];
t2=[0:2*pi/19:2*pi];
t3=[0:2*pi/29:2*pi];
t4=[0:2*pi/39:2*pi];
t5=[0:2*pi/59:2*pi];
t6=[0:2*pi/119:2*pi];
c1=cos(t1);
c1=[c1 c1 c1 c1 c1 c1 c1 c1 c1 c1 c1 c1];
c2=cos(t2);
c2=[c2 c2 c2 c2 c2 c2];
c3=cos(t3);
c3=[c3 c3 c3 c3];
c4=cos(t4);
c4=[c4 c4 c4];
c5=cos(t5);
c5=[c5 c5];
c6=cos(t6);
%Randomfrequencyhoppstoformaspreadsignal
spread_signal=[];
for n=1:25
c=randint(1,1,[1 6]);
switch(c)
case(1)
spread_signal=[spread_signal c1];
case(2)
spread_signal=[spread_signal c2];
case(3)
spread_signal=[spread_signal c3];
case(4)
spread_signal=[spread_signal c4];
case(5)
spread_signal=[spread_signal c5];
case(6)
spread_signal=[spread_signal c6];
end
end
subplot(4,1,3)
plot([1:3000],spread_signal);
axis([-100 3100 -1.5 1.5]);
title('\bf\it Spread Signal with 6 frequencies');
%SpreadingBPSKSignalintowiderbandwithtotalof12frequencies
freq_hopped_sig=bpsk_sig.*spread_signal;
subplot(4,1,4)
plot([1:3000],freq_hopped_sig);
axis([-100 3100 -1.5 1.5]);
title('\bf\it Frequency Hopped Spread Spectrum Signal');
%ExpressingtheFFTs
figure,subplot(2,1,1)
plot([1:3000],freq_hopped_sig);
axis([-100 3100 -1.5 1.5]);
title('\bf\it Frequency Hopped Spread Spectrum signal and its FFT');
subplot(2,1,2);
plot([1:3000],abs(fft(freq_hopped_sig)));