Communication Principles and MATLAB (1): Modulation and Demodulation of AM

1. AM modulation principle

The modulation principle of AM is shown in the figure below. The baseband signal m(t) is added to the DC amount A0, and then multiplied by the high-frequency carrier to realize the modulation of the AM signal.
insert image description here

2. AM demodulation principle

The demodulation principle of AM is shown in the figure below. After the AM signal is transmitted through the channel, it is multiplied by the carrier, and then it is filtered by low pass, and the original baseband signal is restored after DC blocking.
insert image description here

3. Code for AM modulation and demodulation

AM.m file, main file

% AM调制解调过程
%% 基本参数
clear all;                  % 清除变量
close all;                  % 关闭所有窗口图像
fm = 100;                   % 基带信号频率
T = 2;                      % 信号时长
fs = 20000;                 % 采样频率 奈奎斯特采样定理为最大频率的两倍,这里取20倍为了绘制更多的细节,让时域信号更平滑
dt=1/fs;                    % 时间采样间隔,采样频率的倒数
N=T/dt;                     % 采样点个数,总时长除以采样间隔
t=[0:N-1]*dt;               % 采样点的时间序列,作为横坐标

%% ******************调制信号时域波形******************
Am=1;                       % 基带信号幅度 
mt=Am*cos(2*pi*fm*t);       % 基带信号
figure(1);                  % 绘制第一幅图
subplot(221);               % 窗口分割,将一幅图分割成2*2plot(t,mt,'Linewidth',2);   % 时间t为横坐标,基带信号mt为纵坐标绘图,线宽为2
xlabel('t/时间');           % 横坐标标注
ylabel('幅度');             % 纵坐标标注
title('基带信号');          % 图标题标注
axis([0,0.1,-1.1,1.1]);     % 横纵坐标范围设置
line([0,0.1],[0,0],'color','b','Linewidth',2);% 绘制一条从(0,0)(0.1,0)的蓝色实线,线宽为2

%% ******************调制信号频域波形******************
subplot(222);
[mf,msf]=T2F(t,mt);         % 傅里叶变换,得到纵坐标频谱和横坐标频率
plot(mf,abs(msf),'Linewidth',2);    % 画出基带信号频谱,线宽为2
title('基带信号的频谱');            % 图标题标注
xlabel('f/Hz');                     % 横坐标标注
ylabel('幅度/H(f)');                % 纵坐标标注
axis([-150 150 -inf inf]);          % 横纵坐标范围设置

%% ******************载波信号时域波形******************
subplot(223);
fc=1000;                            % 载波频率
zaibo=cos(2*pi*fc*t);               % 载波时域信号
plot(t,zaibo,'r','Linewidth',2);    % 时间t为横坐标,载波信号zaibo为纵坐标绘图,线宽为2,红色
xlabel('t/时间');                   % 横坐标标注
ylabel('幅度');                     % 纵坐标标注
title('载波信号');                  % 图标题标注
axis([0,0.01,-1.1,1.1]);            % 横纵坐标范围设置
line([0,0.01],[0,0],'color','b','Linewidth',2);% 绘制一条从(0,0)(0.01,0)的蓝色实线,线宽为2

%% ******************载波信号频域波形******************
subplot(224);
[mf1,msf1]=T2F(t,zaibo);            % 傅里叶变换,得到纵坐标频谱和横坐标频率
plot(mf1,abs(msf1),'r','Linewidth',2);  % 载波信号频谱,线宽为2,红色
title('载波信号的频谱');            % 图标题标注
xlabel('f/Hz');                     % 横坐标标注
ylabel('幅度/H(f)');                % 纵坐标标注
axis([-1200 1200 -inf inf]);        % 横纵坐标范围设置

%% ******************AM波信号时域波形******************
Ma=0.5;                             % 调制指数
A0=Am/Ma;                           % 直流信号幅度
sam=(A0+mt).*zaibo;                 % 画出AM信号波形
%% 加噪声
SNR=20;                              %信噪比为  dB
sam=awgn(sam,SNR,'measured');
figure(2);                          % 绘制第二幅图
subplot(211);                       % 窗口分割,将一幅图分割成2*1plot(t,sam,'Linewidth',2);          % 画出AM信号波形,线宽为2
hold on;                            % 当前轴及图像保持而不被刷新,准备接受此后将绘制的图形,多图共存
plot(t,A0+mt,'r-');                 % 画出AM信号包络,红色实线
title('调制指数为0.5的AM调制信号及其包络');% 图标题标注
xlabel('t/时间');                   % 横坐标标注
ylabel('幅度');                     % 纵坐标标注
axis([0,0.02,-3.1,3.1]);            % 横纵坐标范围设置
line([0,0.02],[0,0],'color','b','Linewidth',2);% 绘制一条从(0,0)(0.02,0)的蓝色实线,线宽为2

%% ******************AM波信号频域波形******************
[mf2,msf2]=T2F(t,sam);              % 傅里叶变换,得到纵坐标频谱和横坐标频率
subplot(212);
plot(mf2,abs(msf2),'Linewidth',2);  % 画出AM信号频谱
title('AM波信号的频谱');            % 图标题标注
xlabel('f/Hz');                     % 横坐标标注
ylabel('幅度/H(f)');                % 纵坐标标注
axis([-1500 1500 -inf inf]);        % 横纵坐标范围设置

%% ******************相干解调******************
%% ******************已调信号与载波信号相乘******************
st=sam.*zaibo;                      % 已调信号与载波信号相乘
figure(3);                          % 绘制第三幅图
subplot(211);                       % 窗口分割,将一幅图分割成2*1plot(t,st,'Linewidth',2);           % 时间t为横坐标,相乘信号st为纵坐标绘图,线宽为2
title('已调信号与载波信号相乘');    % 图标题标注
xlabel('t/时间');                   % 横坐标标注
ylabel('幅度');                     % 纵坐标标注
axis([0 0.02 -4 4]);                % 横纵坐标范围设置
line([0,0.02],[0,0],'color','b','Linewidth',2);% 绘制一条从(0,0)(0.02,0)的蓝色实线,线宽为2

%% ******************已调信号与载波信号相乘的频谱******************
[f,sf]=T2F(t,st);                   % 傅里叶变换,得到纵坐标频谱和横坐标频率
subplot(212);                       % 窗口分割,将一幅图分割成2*1plot(f,sf,'Linewidth',2);           % 绘制相乘信号st的频谱,线宽为2
title('已调信号与载波信号相乘的频谱');% 图标题标注
xlabel('f/Hz');                     % 横坐标标注
ylabel('幅度/H(f)');                % 纵坐标标注
axis([-2200 2200 -inf inf]);        % 横纵坐标范围设置

%% ******************解调出来的信号******************
[t,st]=lpf(f,sf,2*fm);              % 频域低通滤波
figure(4);                          % 绘制第三幅图
subplot(211);                       % 窗口分割,将一幅图分割成2*1plot(t,2*st-A0,'Linewidth',2);      % 绘制解调波形
title('经过低通滤波的相干解调信号波形');% 图标题标注
xlabel('t/时间');                   % 横坐标标注
ylabel('幅度');                     % 纵坐标标注
axis([0 0.1 -1.1 1.1]);                 % 横纵坐标范围设置 
line([0,0.1],[0,0],'color','b','Linewidth',2);% 绘制一条从(0,0)(0.1,0)的蓝色实线,线宽为2

%% ******************原始信号******************
subplot(212);
plot(t,mt,'r-','Linewidth',2);      % 绘制原始基带信号
title('原始基带信号');                    % 图标题标注
xlabel('t/时间');                   % 横坐标标注
ylabel('幅度');                     % 纵坐标标注
axis([0 0.1 -1.1 1.1]);                 % 横纵坐标范围设置 
line([0,0.1],[0,0],'color','b','Linewidth',2);% 绘制一条从(0,0)(0.1,0)的蓝色实线,线宽为2

T2F.m file, FFT function

function [f,sf]= T2F(t,st)      % FFT
% dt = t(2)-t(1);
T=t(end);                       % 输入信号的时间最大值为T
df = 1/T;                       % dt=1/fs; 时间采样间隔,采样频率的倒数;
                                % N=T/dt;  采样点个数,总时长除以采样间隔
                                % 两式联合推导 df = 1/T 
N = length(st);                 % 输入信号时间的长度为采样点数
f=-N/2*df : df : N/2 * df-df;   % 频率分布
sf = fft(st);                   % 做FFT
sf = T/N * fftshift(sf);        % 最后输出,将0-fs频谱搬移到-fs/2-fs/2频谱

F2T.m file, IFFT function

function[t,st]=F2T(f,Sf)        % IFFT
df=f(2)-f(1);                   % 频率间隔
fmax=(f(end)-f(1)+df);          % 最大频率减最低频率加上频率间隔为带宽
dt=1/fmax;                      % 采样间隔
N=length(f);                    % 采样点数
t=[0:N-1] * dt;                 % 时间分布
Sf=fftshift(Sf);                %0-fs频谱搬移到-fs/2-fs/2频谱
st=fmax * ifft(Sf);             % 做IFFT
st=real(st);                    % 取实部

lpf.m file, low-pass filter function

function[t,st]=lpf(f,sf,B)          % 频率LPF
df=f(2)-f(1);                       % 频率间隔
fN=length(f);                       % 采样点数
ym=zeros(1,fN);                     % 生成1行fN列的0向量
xm=floor(B/df);                     % 低频带宽频率除以间隔后的点数向下取整
xm_shift=[-xm:xm-1]+floor(fN/2);    % 因为前面做FFT将0频率搬移到中心处,
                                    % 因此,低通低频频率相应地搬移fN/2,才是对应的频率点
ym(xm_shift)=1;                     % 低通通过频率处幅度为1,其余为0,相当于理想低通
yf=ym.* sf;                         % FFT信号的频谱和对应低频带宽处频率值为1的行向量相乘
[t,st]=F2T(f,yf);                   % IFFT

Note: These four files should be placed in the same folder, the first one is the main file, and the other three are function files

4. AM modulation and demodulation result graph

The AM signal in the result picture is passed through the channel and Gaussian white noise is added.
If you don't want to add noise, just comment the following line of code.

sam=awgn(sam,SNR,'measured');

insert image description here
insert image description here
insert image description here
insert image description here

5. Advantages and disadvantages of AM

Advantages: Simple implementation, with DC flow for carrier synchronization;
Disadvantages: DC flow takes up a lot of energy, but does not transmit information, and the power utilization rate is low.

Guess you like

Origin blog.csdn.net/qq_47598782/article/details/128300971