首页 > 解决方案 > 如果语句中的 X=0 的值未在 matlab 图中绘制

问题描述

试图从二进制输入中绘制询问、fsk 和 psk 调制的图表。psk 和 fsk 调制图还可以,但 ask 调制图似乎忽略了 0 值并且没有绘制它们。

尝试使用 ask=sin(0); 问=楼梯(0);并反转 if else 语句

prompt = 'Enter bit stream ';
ff = 'Enter space frequency, f = ';
ff2 = 'Enter mark frequency, f2 = ';

x=input(prompt)
f=input(ff)
f2=input(ff2)
nx=size(x,2);

for i=1:1:nx
 t = i:0.01:i+1;
if x(i)==1
   ask=sin(2*pi*f*t);
   fsk=sin(2*pi*f2*t);
   psk=sin(2*pi*f*t);
else
    ask=0;
    fsk=sin(2*pi*f*t);
    psk=sin(2*pi*f*t+pi);
end

subplot(4,1,1);
stairs([x,x(end)]);
hold on;
grid on;
ylabel('Amplitude')
xlabel('Time')
title('Binary Input')
axis([1 nx+3 -2 2]);

subplot(4,1,2);
plot(t,ask);
hold on;
grid on;
ylabel('Amplitude')
xlabel('Time')
title('ASK Modulation')
axis([1 nx+3 -2 2]);

subplot(4,1,3);
plot(t,fsk);
hold on;
grid on;
ylabel('Amplitude')
xlabel('Time')
title('FSK Modulation')
axis([1 nx+3 -2 2]);

subplot(4,1,4);
plot(t,psk);
hold on;
grid on;
ylabel('Amplitude')
xlabel('Time')
title('PSK Modulation')
axis([1 nx+3 -2 2]);

end

这是当前的输出

标签: matlabmatlab-figure

解决方案


每当在 matlab 中绘图时,您的数据需要具有相同的长度。在您的情况下,您试图在整个 t 向量上绘制一个 0 以进行询问。您需要将其更改为零向量,如下所示:

prompt = 'Enter bit stream ';
ff = 'Enter space frequency, f = ';
ff2 = 'Enter mark frequency, f2 = ';

x=input(prompt)
f=input(ff)
f2=input(ff2)
nx=size(x,2);

for i=1:1:nx
    t = i:0.01:i+1;
    if x(i)==1
        ask=sin(2*pi*f*t);
        fsk=sin(2*pi*f2*t);
        psk=sin(2*pi*f*t);
    else
        ask=zeros(length(t), 1);
        fsk=sin(2*pi*f*t);
        psk=sin(2*pi*f*t+pi);
    end

    subplot(4,1,1);
    stairs([x,x(end)]);
    hold on;
    grid on;
    ylabel('Amplitude')
    xlabel('Time')
    title('Binary Input')
    axis([1 nx+3 -2 2]);

    subplot(4,1,2);
    plot(t,ask);
    hold on;
    grid on;
    ylabel('Amplitude')
    xlabel('Time')
    title('ASK Modulation')
    axis([1 nx+3 -2 2]);

    subplot(4,1,3);
    plot(t,fsk);
    hold on;
    grid on;
    ylabel('Amplitude')
    xlabel('Time')
    title('FSK Modulation')
    axis([1 nx+3 -2 2]);

    subplot(4,1,4);
    plot(t,psk);
    hold on;
    grid on;
    ylabel('Amplitude')
    xlabel('Time')
    title('PSK Modulation')
    axis([1 nx+3 -2 2]);

end

现在你得到这样的东西:

在此处输入图像描述


推荐阅读