Search code examples
matlabmatlab-figure

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


Trying to plot a graph for ask, fsk & psk modulation from binary input. The graphs of psk and fsk modulation are ok, but the ask modulation graph seems to ignore the 0 values and doesn't plot them.

tried using ask=sin(0); ask=stairs(0); and reversing the if else statement

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


Solution

  • Whenever plotting in matlab your data need to be the same length. In your case, you're trying to plot a single 0 over your entire t vector for ask. You need to change this to a zeros vector, like this:

    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
    

    Now you get something like this:

    enter image description here