if 语句中 X=0 的值未在 matlab 图中绘制

value of X=0 from if statement is not plotting in matlab graph

正在尝试绘制二进制输入的询问、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

Here's the current output

无论何时在 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

现在你得到这样的东西: