本人MATLAB学习小白,仅做笔记记录和分享~~
clc;
close all;
Ts=1;
N_sample=8;
dt=Ts/N_sample;
N=1000;
t=0:dt:(N*N_sample-1)*dt;
%码型构建%
gt1=ones(1,N_sample); % 1s时长高电平 NRZ波形
gt2=[ones(1,N_sample/2),zeros(1,N_sample/2)]; % RZ波形
mt3=sinc((t-5)/Ts);
gt3=mt3(1:1000*N_sample);
%plot(t(1:10*N_sample),gt3);
d=(sign(randn(1,N))+1)/2; %生成1*N的均值为0,方差为1的随机数;sign函数返回值有3种,+1、-1、0;取0值概率为0
data=sigxpand(d,N_sample); %等间隔插入7个0
st1=conv(data,gt1);
st2=conv(data,gt2);
d=2*d-1;
data=sigxpand(d,N_sample);
st3=conv(data,gt3);
figure(1);
subplot(321),plot(t(1:1000),st1(1:1000));axis([0,t(1000),0,2]);
subplot(323),plot(t(1:1000),st2(1:1000));axis([0,t(1000),0,2]);
subplot(325),plot(t(1:1000),st3(1:1000));axis([0,t(1000),-3,3]);
[f,stf1]=T2F(t,st1(1:length(t)));
[f,stf2]=T2F(t,st2(1:length(t)));
[f,stf3]=T2F(t,st3(1:length(t)));
subplot(322),plot(f,10*log10(abs(stf1).^2));grid;
axis([-5,5,-40,100]);xlabel('单极性NRZ信号功率谱密度');
subplot(324),plot(f,10*log10(abs(stf2).^2));grid;
axis([-5,5,-40,100]);xlabel('单极性RZ信号功率谱密度');
subplot(326),plot(f,10*log10(abs(stf3).^2));grid;
axis([-5,5,-40,100]);xlabel('sinc波形功率谱密度');
%%%%%%%----函数定义必须在结尾-----%%%%%%%%%%%%%%%%%%
function[out]=sigxpand(d,M)
N=length(d);
out=zeros(M,N);
out(1,:)=d;
out=reshape(out,1,M*N);
end
%%%%%%%--------end--------------%%%%%%%%%%%%%%%%%%
%------------------FFT变换函数--------------%
function[f,sf]=T2F(t,st)
dt=t(2)-t(1);
N=length(st);
fs=1/dt;
df=fs/N;
f=0:df:(N-1)*df;
sf=fft(st);
sf=fftshift(sf);
f=f-N/2*df;
end
%------------------end-----------------%
1、生成0 1相间的随机序列
d=(sign(randn(1,N))+1)/2;
% or
d=randi([0,1],1,N);
2、同理生成-1 1相间的随机序列
d=randi([0,1],1,N);
d=2*d-1; %在上述的基础上,加上此语句
3、为什么要等间隔插入N_sample-1个0呢
gt1=[1,1,1] %基本码元
data=[1,0,1,1,1,0,1]; %码元序列
st1=conv(gt1,data);
plot(st1);
clc;
close all;
gt1=[1,1,1]; %基本码元
data=[1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0]; %码元序列
st1=conv(gt1,data);
plot(st1);
axis([1,24,0,1.5]);grid;