调制方式matlab代码_基础的几种实现

只是实现调制,不做解调。只做了代码实现,没有对此进行解释,请自行再次验证算法准确和是否符合您的预期。

2ASK

function [ Modulated_signal,time_seq ] = Two_ASK( seq,fc,fs,fd,Ac)
    % UNTITLED 此处显示有关此函数的摘要
    % seq是待调制基带序列,fc是载波频率,fs为采样频率,fd为的发送基带码元的速率,Ac为调制信号最大幅值
    len_seq = length(seq);
    Duration = len_seq/fd;
    time_axis = 0:1/fs:Duration;
    %base_endtime_axis = 1/fd:1/fd:Duration;
    reflict_index = zeros(1,length(time_axis));
    for i = 1:length(time_axis)
        reflict_index(i) = fix(time_axis(i)/(1/fd)) + 1;
    end
    reflict_index(end) = reflict_index(end) - 1;
    Carrier = Ac*cos(2*pi*fc*time_axis);
    Modulated_signal = zeros(1,length(Carrier));
    for i = 1 : length(Modulated_signal)
        Modulated_signal(i) = Carrier(i)*seq(reflict_index(i));
    end
    time_seq = time_axis;
end

4ASK

function [  Modulated_signal,time_seq ] = Four_ASK( seq,fc,fs,fd,Ac)
    % UNTITLED 此处显示有关此函数的摘要
    % seq是待调制基带序列,fc是载波频率,fs为采样频率,fd为的发送基带码元的速率,Ac为单边幅值差,可设为2
    % 请保证seq长度为偶数
    if mod(length(seq),2) ~= 0
        Modulated_signal = 0;
        time_seq = 0;
    else
        single_seq = zeros(1,length(seq)/2);
        double_seq = zeros(1,length(seq)/2);
        for i = 1:length(seq)
            if mod(i,2) == 0
                double_seq(i/2) = seq(i);
            else
                single_seq(fix(i/2)+1) = seq(i);
            end
        end
        [s_seq,t_s] = Two_ASK( single_seq,fc,fs,fd/2,1);
        [d_seq,t_d] = Two_ASK( double_seq,fc,fs,fd/2,1);
        Modulated_signal = s_seq + d_seq*Ac;
        time_seq = t_s;
    end


end


2FSK

function [ Modulated_signal,time_seq ] = Two_FSK( seq,fc1,fc2,fs,fd,Ac)
    %UNTITLED3 此处显示有关此函数的摘要
    %   此处显示详细说明
    reserver_seq = 1 - seq;
    len_seq = length(seq);
    Duration = len_seq/fd;
    time_axis = 0:1/fs:Duration;
    %base_endtime_axis = 1/fd:1/fd:Duration;
    reflict_index = zeros(1,length(time_axis));
    for i = 1:length(time_axis)
        reflict_index(i) = fix(time_axis(i)/(1/fd)) + 1;
    end
    reflict_index(end) = reflict_index(end) - 1;
    Carrier_fc1 = Ac*cos(2*pi*fc1*time_axis);
    Carrier_fc2 = Ac*cos(2*pi*fc2*time_axis);
    Modulated_signal = zeros(1,length(Carrier_fc2));
    for i = 1 : length(Modulated_signal)
        Modulated_signal(i) = Carrier_fc1(i)*seq(reflict_index(i)) + Carrier_fc2(i)*reserver_seq(reflict_index(i));
    end
    time_seq = time_axis;

end




4FSK

function [ Modulated_signal,time_seq  ] = Four_FSK( seq,fc1,fc2,fc3,fc4,fs,fd,Ac )
    %UNTITLED4 此处显示有关此函数的摘要
    %   此处显示详细说明
    % 请保证seq长度为偶数
    if mod(length(seq),2) ~= 0
        Modulated_signal = 0;
        time_seq = 0;
    else
        single_seq = zeros(1,length(seq)/2);
        double_seq = zeros(1,length(seq)/2);
        for i = 1:length(seq)
            if mod(i,2) == 0
                double_seq(i/2) = seq(i);
            else
                single_seq(fix(i/2)+1) = seq(i);
            end
        end
        len_seq = length(single_seq);
        Duration = len_seq/(fd/2);
        time_axis = 0:1/fs:Duration;
        %base_endtime_axis = 1/fd:1/fd:Duration;
        reflict_index = zeros(1,length(time_axis));
        for i = 1:length(time_axis)
            reflict_index(i) = fix(time_axis(i)/(1/fd*2)) + 1;
        end
        reflict_index(end) = reflict_index(end) - 1;
        Carrier_fc1 = Ac*cos(2*pi*fc1*time_axis);
        Carrier_fc2 = Ac*cos(2*pi*fc2*time_axis);
        Carrier_fc3 = Ac*cos(2*pi*fc3*time_axis);
        Carrier_fc4 = Ac*cos(2*pi*fc4*time_axis);
        Modulated_signal = zeros(1,length(Carrier_fc4));
        for i = 1 : length(Carrier_fc4)
            if single_seq(reflict_index(i)) == 0 && double_seq(reflict_index(i)) == 0
                Modulated_signal(i) = Carrier_fc1(i);
            elseif single_seq(reflict_index(i)) == 0 && double_seq(reflict_index(i)) == 1
                Modulated_signal(i) = Carrier_fc2(i);
            elseif single_seq(reflict_index(i)) == 1 && double_seq(reflict_index(i)) == 0
                Modulated_signal(i) = Carrier_fc3(i);
            else
                Modulated_signal(i) = Carrier_fc4(i);
            end
        end
        time_seq = time_axis;
    end


end


2PSK

function [ Modulated_signal,time_seq ] = Two_PSK(seq,fc,fs,fd,Ac)
    %UNTITLED 此处显示有关此函数的摘要
    %   此处显示详细说明
    reserver_seq = 1 - seq;
    len_seq = length(seq);
    Duration = len_seq/fd;
    time_axis = 0:1/fs:Duration;
    %base_endtime_axis = 1/fd:1/fd:Duration;
    reflict_index = zeros(1,length(time_axis));
    for i = 1:length(time_axis)
        reflict_index(i) = fix(time_axis(i)/(1/fd)) + 1;
    end
    reflict_index(end) = reflict_index(end) - 1;
    Carrier1 = Ac*cos(2*pi*fc*time_axis);
    Carrier2 = Ac*sin(2*pi*fc*time_axis);
    Modulated_signal = zeros(1,length(Carrier1));
    for i = 1 : length(Modulated_signal)
        Modulated_signal(i) = Carrier1(i)*seq(reflict_index(i)) + Carrier2(i)*reserver_seq(reflict_index(i));
    end
    time_seq = time_axis;

end


4PSK

function [ Modulated_signal,time_seq ] = Four_PSK( seq,fc,fs,fd,Ac )
    %UNTITLED2 此处显示有关此函数的摘要
    %   此处显示详细说明
    %UNTITLED4 此处显示有关此函数的摘要
    %   此处显示详细说明
    % 请保证seq长度为偶数
    if mod(length(seq),2) ~= 0
        Modulated_signal = 0;
        time_seq = 0;
    else
        single_seq = zeros(1,length(seq)/2);
        double_seq = zeros(1,length(seq)/2);
        for i = 1:length(seq)
            if mod(i,2) == 0
                double_seq(i/2) = seq(i);
            else
                single_seq(fix(i/2)+1) = seq(i);
            end
        end
        len_seq = length(single_seq);
        Duration = len_seq/(fd/2);
        time_axis = 0:1/fs:Duration;
        %base_endtime_axis = 1/fd:1/fd:Duration;
        reflict_index = zeros(1,length(time_axis));
        for i = 1:length(time_axis)
            reflict_index(i) = fix(time_axis(i)/(1/fd*2)) + 1;
        end
        reflict_index(end) = reflict_index(end) - 1;
        Carrier_fc1 = Ac*cos(2*pi*fc*time_axis);
        Carrier_fc2 = Ac*cos(2*pi*fc*time_axis + pi/2);
        Carrier_fc3 = Ac*cos(2*pi*fc*time_axis + pi);
        Carrier_fc4 = Ac*cos(2*pi*fc*time_axis + 3*pi/2);
        Modulated_signal = zeros(1,length(Carrier_fc4));
        for i = 1 : length(Carrier_fc4)
            if single_seq(reflict_index(i)) == 0 && double_seq(reflict_index(i)) == 0
                Modulated_signal(i) = Carrier_fc1(i);
            elseif single_seq(reflict_index(i)) == 1 && double_seq(reflict_index(i)) == 0
                Modulated_signal(i) = Carrier_fc2(i);
            elseif single_seq(reflict_index(i)) == 1 && double_seq(reflict_index(i)) == 1
                Modulated_signal(i) = Carrier_fc3(i);
            else
                Modulated_signal(i) = Carrier_fc4(i);
            end
        end
        time_seq = time_axis;
    end
    

end


demo.m中调用它们

clc;
clear all;
seq = randi(2,1,100) - 1;
%[1,0,0,1,1,0,1,1,1,0,0,0,1,0,0,1];
fc = 100;
fs = 400;
fd = 20;
Ac = 1;

%% MASK
[sig2ask,t2ask] = Two_ASK( seq,fc,fs,fd,Ac);
[sig4ask,t4ask] = Four_ASK( seq,fc,fs,fd,2);

figure(1);
plot(t2ask,sig2ask);
xlabel('Time(s)');
ylabel('Value(v)');
title('2ASK');

figure(2);
plot(t4ask,sig4ask);
xlabel('Time(s)');
ylabel('Value(v)');
title('4ASK');

%% MFSK
[sig2fsk,t2fsk] = Two_FSK(seq,100,200,400,fd,Ac);
[sig4fsk,t4fsk] = Four_FSK(seq,100,200,40,80,800,fd*2,Ac);


figure(3);
plot(t2fsk,sig2fsk);
xlabel('Time(s)');
ylabel('Value(v)');
title('2FSK');

figure(4);
plot(t4fsk,sig4fsk);
xlabel('Time(s)');
ylabel('Value(v)');
title('4FSK');

%% MPSK
[sig2psk,t2psk] = Two_PSK( seq,fc/2,fs/2,fd,Ac);
[sig4psk,t4psk] = Four_PSK(seq,fc/2,fs/2,fd,Ac);

figure(5)
plot(t2psk,sig2psk);
xlabel('Time(s)');
ylabel('Value(v)');
title('2PSK');

figure(6)
plot(t4psk,sig4psk);
xlabel('Time(s)');
ylabel('Value(v)');
title('4PSK');

结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
给出代码的百度云链接
链接:https://pan.baidu.com/s/1r7SDF2n3iXGq7gM8cEcV-Q
提取码:TZ33

注意,上述仿真是基于最基础的数字信号处理与信号系统编写的信号调制代码,过于理想,合理的信号调制过程还包括脉冲成型等,代码仅供学习。

  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值