matlab仿真笔记

离散信号和系统
例3.1画出x=cos2t,0<=t<=2pi的抽样序列,抽样周期为0.1
t=0:0.1:2pi;
x=cos(2
t);
stem(t,x);
%plot(t,x);

例3.2画出信号x1=sin(2pi0.1n)与信号x2=exp(-0.1n),0<=n<=40及它们相加和相乘序列。
n=0:40;
x1=sin(2
pi0.1n);
x2=exp(-0.1*n);
y1=x1+x2;
y2=x1.*x2;
subplot(2,2,1);stem(n,x1);title(‘x1’);
subplot(2,2,2);stem(n,x2);title(‘x2’);
subplot(2,2,3);stem(n,y1);title(‘y1’);
subplot(2,2,4);stem(n,y2);title(‘y2’);

例3.3求卷积和
clear ;
clc;
n=0:40;
h=exp(-0.1n);
x=exp(-0.2
n);
y=conv(x,h);
subplot(1,1,1);stem(y);

例3.4离散时间系统问题.一阶自回归差分方程,有输出到输入的反馈,为无限冲激响应系统iir
n=50;
x1=zeros(1,n);
x1(1)=1;
y1(1)=x1(1);
x2=zeros(1,n);
x2(1:41)=exp(-0.1*(0:40));
y2(1)=x2(1);
for i=2:n
y1(i)=0.8y1(i-1)+x1(i);
y2(i)=0.8
y2(i-1)+x2(i);
end
subplot(4,1,1);stem(x1);title(‘x1’);
subplot(4,1,2);stem(x2);title(‘x2’);
subplot(4,1,3);stem(y1);title(‘y1’);
subplot(4,1,4);stem(y2);title(‘y2’);

例3.5FIR系统
b(1)=1/2;
b(2)=1/8;
b(3)=3/8;
f(1:6)=1;
g=conv(f,b);
subplot(1,1,1);stem(g);title(‘y’);

例3.6傅里叶分析
syms t;
f=t*exp(-abs(t));
F=fourier(f)
subplot(2,1,1);fplot(f);title(‘f’);
subplot(2,1,2);fplot(abs(F));title(‘F’);

例3.7傅里叶逆变换
syms t w;
F=pi*exp(-abs(w));
f=ifourier(F)
subplot(2,1,1);fplot(F);
subplot(2,1,2);fplot(f);

例3.8周期函数的傅里叶级数,信号的离散谱

例3.15解析信号(预包络),时域中实部为本身,虚部为本身除以pit(希尔伯特变换),希尔伯特变换器的冲击响应为h(t)=1/pit.希尔伯特变换函数hilbert产生解析信号,而不是希尔伯特变换。

例3.17自相关函数与功率谱密度
n=100;
N=2000;
X=randn(n,N);
for i=1:n
[RX(i,:),lags]=xcorr(X(i,:),50,‘coeff’);
SF(i,:)=fftshift(abs(fft(RX(i,:))));
end
RX_av=sum(RX)/n;
SF_av=sum(SF)/n;
subplot(2,1,1);plot(lags,RX_av);
subplot(2,1,2);plot(lags,SF_av);
axis([-50 50 0 2]);

例3.18带通随机过程
ts=0.002;
tao=-1:ts:1;
fs=1/ts;
df=fs/length(tao);
f=-fs/2:df:fs/2-df;
R=sinc(220tao).cos(2pi100tao);
Rf=fft®/fs;
subplot(2,1,1);plot(tao,R);xlabel(‘tao’);ylabel(‘R’);
subplot(2,1,2);plot(f,fftshift(abs(Rf)));xlabel(‘f’);ylabel(‘Rf’);
信道
例4.1awgn(x,snr)函数,snr为信噪比(dB),x功率为0dB,噪声实际功率为-snr;
t=0:0.001:10;
x=sin(2pit);
rx=awgn(x,20);
subplot(2,1,1);plot(t,x);
subplot(2,1,2);plot(t,rx);
var(rx-x)

例4.2
awgn(x,snr,sigpower),sigpower为输入信号的功率。
t=0:0.001:10;
x=sin(2pit);
rx=awgn(x,20,10);
subplot(2,1,1);plot(t,x);
subplot(2,1,2);plot(t,rx);
var(rx-x)

例4.3awgn(x,snr,‘measured’),首先计算输入信号x的功率,再按照snr添加相应功率的高斯白噪声。
t=0:0.001:10;
x=sin(2pit);
rx=awgn(x,20,‘measured’);
subplot(2,1,1);plot(t,x);
subplot(2,1,2);plot(t,rx);
var(rx-x)

例4.4awgn(x,snr,…,state),state为随机数种子,state一致时产生的随机数一致。
t=1:10;
snr=10;
x1=awgn(t,snr,‘measured’,10);
x2=awgn(t,snr,‘measured’,10);
x3=awgn(t,snr,‘measured’,5);

例4.5 randn函数,产生高斯分布
t=0:0.001:10;
x=sin(2pit);
snr=20;
xp=norm(x).^2/length(x)
np=xp./10.^(snr/10);
n=sqrt(np).*randn(1,length(x));
y=x+n;
subplot(2,1,1);plot(t,x);
subplot(2,1,2);plot(t,y);
var(n)

例4.6AWGN仿真
QPSK调制的基带数字通信系统通过awgn信道的误符号率SER和误比特率BER,发射端信息比特采用Gray编码影射,基带脉冲采用矩形脉冲,每个脉冲抽样点数为8,接收端采用匹配滤波器进行相干解调。
nSamp=8;
numSymbol=2000;
M=4;
grayencod=[0 1 3 2];
snr=-3:3;
for ii=1:length(snr)
msg=randsrc(1,numSymbol,[0:3]);
msg_gr=grayencod(msg+1);
msg_txo=pskmod(msg_gr,M);
msg_tx=rectpulse(msg_txo,nSamp);
msg_rx=awgn(msg_tx,snr(ii),‘measured’);
msg_rx_down=intdump(msg_rx,nSamp);
msg_gr_decod=pskdemod(msg_rx_down,M);
[dummy graydecod]=sort(grayencod);
graydecod=graydecod-1;
msg_demod=graydecod(msg_gr_decod+1);
[errorBit,Ber(ii)]=biterr(msg,msg_demod,log2(M));
[errorSym,Ser(ii)]=symerr(msg,msg_demod);
end
scatterplot(msg_tx(1:100));
title(‘发射信号星座图’);xlabel(‘同相分量’);ylabel(‘正交分量’);
scatterplot(msg_rx(1:100));
title(‘接收信号星座图’);xlabel(‘同相分量’);ylabel(‘正交分量’);
figure;
semilogy(snr,Ber,’-ro’,snr,Ser,’-r*’);
legend(‘Ber’,‘Ser’);
title(‘QPSK在AWGN信道下的性能’);
xlabel(‘信噪比(dB)’);ylabel(‘误符号率和误比特率’);
数字基带传输
例7.1载波PAM仿真
nsymbol=100000;
T=1;
fs=100;
ts=1/fs;
t=0:ts:T-ts;
fc=10;
c=sqrt(2/T)cos(2pifct);
M=4;
graycode=[0 1 3 2];
EsN0=0:15;
snr1=10.^(EsN0./10);
msg=randi([0,3],1,nsymbol);
msg1=graycode(msg+1);
msgmod=pammod(msg1,M).’;
tx=msgmodc;
tx1=reshape(tx.’,1,length(msgmod)length©);
spow=norm(tx1).^2/nsymbol;
for ii=1:length(EsN0)
sigma=sqrt(spow/(2
snr1(ii)));
rx=tx1+sigma
randn(1,length(tx1));
rx1=reshape(rx,length©,length(msgmod));
y=(crx1)/length©;
y1=pamdemod(y,M);
decmsg=graycode(y1+1);
[err,ber(ii)]=biterr(msg,decmsg,log2(M));
[err,ser(ii)]=symerr(msg,decmsg);
end
semilogy(EsN0,ber,’-ko’,EsN0,ser,’-k
’,EsN0,1.5qfunc(sqrt(0.4snr1)));
title(‘4-PAM载波调制信号在AWGN信道下的性能’);
xlabel(‘Es/N0’);ylabel(‘误比特率和误符号率’);
legend(‘误比特率’,‘误符号率’,‘理论误符号率’)

例7.8QAM信号的仿真
M=8;
msg=[1 4 3 0 7 5 2 6];
ts=0.01;
T=1;
t=0:ts:T;
x=0:ts:length(msg);
fc=1;
c=sqrt(2)exp(1i2pifct);
msg_qam=qammod(msg,M).’;
tx_qam=real(msg_qam
c);
tx_qam=reshape(tx_qam.’,1,length(msg)*length(t));
plot(x,tx_qam(1:length(x)));
title(‘8QAM信号波形’);
xlabel(‘时间’);ylabel(‘载波振幅’);
scatterplot(msg_qam);
title(‘8QAM信号星座图’);
xlabel(‘同相分量’);ylabel(‘正交分量’);

例7.9用基带等效的方式仿真16-QAM载波调制信号在AWGN信道下的误码率和误比特率性能。
%基带等效方式仿真16-QAM载波调制信号在AWGN信道下的误比特率性能
clear all
nsymbol = 100000; %每种SNR下发送的符号数

M=16; %16-QAM
graycode = [0 1 3 2 4 5 7 6 12 13 15 14 8 9 11 10];
EsN0 = 5:20;
snr1 = 10.^(EsN0/10); %信噪比转换为线性值
msg = randi([0,1],1,nsymbol); %消息数据
msg1 = graycode(msg+1); % Gray映射
msgmod = qammod(msg1, M); %16-QAM调制
spow = norm(msgmod).^2/nsymbol; %求每个符号的平均gonglv

for indx=1:length(EsN0)
sigma = sqrt(spow/(2snr1(indx))); %根据符号功率求噪声功率
rx = msgmod+sigma
(randn(1,length(msgmod))+j*randn(1,length(msgmod))); %加高斯白噪声
y = qamdemod(rx,M); %判决
demsg = graycode(y+1);
[err,ber(indx)] = biterr(msg,demsg,log2(M));
[err,ser(indx)] = symerr(msg,demsg);
end

P4 = 2*(1-1/sqrt(M))qfunc(sqrt(3snr1/(M-1)));
ser1 = 1-(1-P4).^2;
ber1 = 1/log2(M)ser1;
semilogy(EsN0,ber,"-ko", EsN0, ser, "-k
", EsN0, ser1, “-r*”, EsN0, ber1, “-ro”);
title(“16-QAM载波调制信号在AWGN信道下的误比特率性能”)
xlabel(“EbN0”);
ylabel(“误比特率P和误符号率”);
legend(“误比特率”, “误符号率”, “理论误符号率”, “理论误比特率”)

你可能感兴趣的:(matlab仿真笔记)