+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Member level 4
    Points: 1,119, Level: 7

    Join Date
    Apr 2010
    Location
    Mumbai, India
    Posts
    68
    Helped
    3 / 3
    Points
    1,119
    Level
    7

    error in ASK-4 Matlab code

    Hello All,

    The code for ASK-4 is as follows:

    N = 8; % The number of bits to send - Frame Length
    bit_stream = round(rand(1,N)) % Random bit stream
    A1 = 3; % Amplitude for 0 bit
    A2 = 5; % Amplitude for 1 bit
    f = 3; % Frequency of Modulating Signal
    fs = 100; % Sampling rate
    t = 0:1/fs:1; % Time for 1 bit
    % This time variable is just for plot
    time = [];
    ASK4_signal = [];
    for ii = 1: 1: length(bit_stream)
    ASK4_signal = [ASK4_signal (bit_stream(ii)==0)&&(bit_stream(ii)==0)*A1*sin(2* pi*f*t)+(bit_stream(ii)==0)&&(bit_stream(ii)==1)*A 2*sin(2*pi*f*t)+(bit_stream(ii)==1)&&(bit_stream(i i)==0)*-A2*sin(2*pi*f*t)+(bit_stream(ii)==1)&&(bit_stream( ii)==1)*-A1*sin(2*pi*f*t)];
    time = [time t];
    t = t + 1;
    end
    subplot(2,1,1);
    plot(time,ASK4_signal,'LineWidth',2);
    xlabel('Time (bit period)');
    ylabel('Amplitude');
    title('ASK4');
    %axis([0 time(end) 1.5 1.5]);
    grid on;

    When I run the above code, it gives me following error as
    Operands to the || and && operators must be convertible to logical scalar
    values.

    Error in Untitled3 (line 12)
    ASK4_signal = [ASK4_signal
    (bit_stream(ii)==0)&&(bit_stream(ii)==0)*A1*sin(2* pi*f*t)+(bit_stream(ii)==0)&&(bit_stream(ii)==1)*A 2*sin(2*pi*f*t)+(bit_stream(ii)==1)&&(bit_stream(i i)==0)*-A2*sin(2*p

    I am not able to understand what is wrong in the code. Kindly help me.

    Regards,
    Shruti

    •   AltAdvertisment

        
       

  2. #2
    Newbie level 2
    Points: 230, Level: 2

    Join Date
    Jul 2012
    Posts
    2
    Helped
    0 / 0
    Points
    230
    Level
    2

    Re: error in ASK-4 Matlab code

    it seems that a comma is missing in: (bit_stream(i i)

    Try the following:

    N = 8; % The number of bits to send - Frame Length
    bit_stream = round(rand(1,N)) % Random bit stream
    A1 = 3; % Amplitude for 0 bit
    A2 = 5; % Amplitude for 1 bit
    f = 3; % Frequency of Modulating Signal
    fs = 100; % Sampling rate
    t = 0:1/fs:1; % Time for 1 bit
    % This time variable is just for plot
    time = [];
    ASK4_signal = [];
    for ii = 1: 1: length(bit_stream)
    ASK4_signal = [ASK4_signal (bit_stream(ii)==0)&&(bit_stream(ii)==0)*A1*sin(2* pi*f*t)+(bit_stream(ii)==0)&&(bit_stream(ii)==1)*A 2*sin(2*pi*f*t)+(bit_stream(ii)==1)&&(bit_stream(i ,i)==0)*-A2*sin(2*pi*f*t)+(bit_stream(ii)==1)&&(bit_stream( ii)==1)*-A1*sin(2*pi*f*t)];
    time = [time t];
    t = t + 1;
    end
    subplot(2,1,1);
    plot(time,ASK4_signal,'LineWidth',2);
    xlabel('Time (bit period)');
    ylabel('Amplitude');
    title('ASK4');
    %axis([0 time(end) 1.5 1.5]);
    grid on;



    •   AltAdvertisment

        
       

  3. #3
    Junior Member level 1
    Points: 99, Level: 1

    Join Date
    Sep 2013
    Posts
    17
    Helped
    0 / 0
    Points
    99
    Level
    1

    Re: error in ASK-4 Matlab code

    send you my code:
    close all
    clear all
    clc
    %ASK..............
    SNR=linspace(10,30,10);%dB
    N=10^6;% number of bit
    a1=3;a2=8; %amplitude for 0 bit & 1 bit.
    A=[3 8];
    f=3; % carrier frequency.
    n=30;% number sample of bit.
    bit_stream= round(rand(1,N));
    fres=A(bit_stream+1);
    bit_stream_sig= rectpulse(bit_stream,n);
    t=linspace(0,1,30);carrier0=3*sin(2*pi*f*t);carrie r1=a2*sin(2*pi*f*t);
    % ask signal
    for i=1:length(bit_stream)
    if bit_stream(i)==0
    ask_signal((i-1)*30+1:i*30)=carrier0;
    else
    ask_signal((i-1)*30+1:i*30)=carrier1;
    end
    end
    plot(bit_stream_sig(1:8*30));hold on;plot(ask_signal(1:8*30),'r');
    for ii=1:length(SNR)
    ask_receive=awgn(ask_signal,SNR(ii),'measured');
    %recover ask signal
    ask_receive=ask_receive';
    ask_receive = reshape(ask_receive,n,N);
    [a b]= size(ask_receive);
    for i=1:b
    ask_receive(:,i)=ask_receive(:,i).*sin(2*pi*f*t)';
    end;
    ask_receive=round(mean(ask_receive,1)*2);
    %bit error
    N_err=biterr(ask_receive,fres);
    BER(ii)=N_err/N;
    end
    figure(2)
    semilogy(SNR,BER)
    grid on
    I would like this code can help you!have fun



--[[ ]]--