Matlab Signal Representation Examples

Embed Size (px)

Citation preview

  • 8/2/2019 Matlab Signal Representation Examples

    1/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia%% Example 1 [Time domain for analogue signals]

    clear all;close all;clc;

    Fs=8000; % Sampling Frequency

    fm=10; % the signal's frequency in Hz.t1=[0:0.1*Fs]'/Fs; % Sampling time for 0.1 second

    t2=[0:Fs]'/Fs; % Time vector for all the signal

    x=sin(2*pi*fm*t1); % representation of 0.1 second of signal x

    % so, there are one cycle in 0.1 second %because

    fm=10 Hz

    x2=sin(2*pi*fm*t2); % representation of 1 second of signal x

    % Hence, there are 10 cycles in each second according

    % to its frequency fm=10 Hz.

    subplot(211); plot(t1,x);grid;

    subplot(212);,plot(t2,x2);grid;

    1

  • 8/2/2019 Matlab Signal Representation Examples

    2/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia%% Example 2 [Time domain for analoge signals]

    clear all;close all;clc;

    Fs=8000; % Sampling Frequency

    fm=100; % the signal's frequency in Hz.

    Fc=2000; % Carrier frequency is 2 kHz.t1=[0:0.1*Fs]'/Fs; % Sampling time for 0.1 second

    t2=[0:Fs]'/Fs; % Time vector for all the signal

    x=sin(2*pi*fm*t1); % representation of 0.1 second of signal x

    % so, there are 10 cycles in 0.1 second %because fm=100 Hz

    y=x.*sin(2*pi*Fc*t1); % Modulated signal Fc=2 kHz.

    x2=sin(2*pi*fm*t2); % representation of 1 second of signal x

    % Hence, there are 100 cycles in each second %according

    % to its frequency fm=100 Hz.

    y2=x2.*sin(2*pi*Fc*t2); % Modulated signal Fc=2 kHz.

    subplot(221); plot(t1,x,'r');grid;

    subplot(222);,plot(t1,y,'r');grid;subplot(223); plot(t2,x2,'k');grid;

    subplot(224);,plot(t2,y2,'k');grid;

    %% Example 3 [Time/Frequency domain for analoge signals]

    clear all;close all;clc;

    Fs=1000; % Sampling Frequency

    T=1/Fs; % sampling time

    L=1000; % length of signal

    %% Time Domain Plot

    t=[0:L-1]*T; % time vector

    2

  • 8/2/2019 Matlab Signal Representation Examples

    3/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysiax=0.7*sin(2*pi*150*t)+sin(2*pi*250*t); % Original Signal

    y=x+2*randn(size(t)); % Noisy Signal

    subplot(221); plot(Fs*t(1:50),x(1:50),'r');grid; xlabel('time in ms')

    subplot(222);,plot(Fs*t(1:50),y(1:50),'r');grid; xlabel('time in ms')

    %% Frequency Domain PlotNFFT=L; % Length of FFT

    X=fft(y,NFFT)/L; % Fourier Transform of signal x

    Y=fft(y,NFFT)/L; % Fourier Transform of signal y

    f=Fs/2*linspace(0,1,NFFT/2+1); % Frequency index

    subplot(223);plot(f,2*abs(X(1:NFFT/2+1)));grid; xlabel('Frequency in Hz')

    subplot(224);plot(f,2*abs(Y(1:NFFT/2+1)));grid; xlabel('Frequency in Hz')

    3

  • 8/2/2019 Matlab Signal Representation Examples

    4/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia%% Example 4 [Time/Frequency domain for analoge signals]

    clear all;close all;clc;

    Fs=16000; % Sampling Frequency

    Fm=3.4e3; % message frequency

    t=(0:0.01*Fs)'/Fs; % time indexingx=0.35*sin(2*pi*Fm*t); % Original Signal

    L=length(t);

    X=fft(x,L)/L;

    f=Fs/2*linspace(0,1,L/2+1); % Frequency index

    subplot(211); plot(t,x,'r');grid;

    subplot(212);plot(f,2*abs(X(1:L/2+1)));grid; xlabel('Frequency in Hz')

    4

  • 8/2/2019 Matlab Signal Representation Examples

    5/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia%% Example 5 [Time/Frequency domain for analoge signals]

    clear all;close all;clc;

    Fs=8000; % Sampling Frequency

    Fm=1750; % message frequency

    t=(0:0.01*Fs)'/Fs; % time indexingN=length(t);

    x=0.8*square(2*pi*Fm*t); % Original Signal

    X=fft(x,N)/N;

    f=Fs/2*linspace(0,1,N/2+1); % Frequency index

    subplot(211); plot(t,x,'r');grid;

    subplot(212);plot(f,2*abs(X(1:N/2+1)));grid; xlabel('Frequency in Hz')

    5

  • 8/2/2019 Matlab Signal Representation Examples

    6/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia%% Example 6 [Time domain for Digital signals]

    clear all;close all;clc;

    Ts=1/1000; % Sampling Period is 1 ms

    N=2000; % Number of bits are 2000 bits

    data=randi([0 1],N,1);t=(0:1:N-1)*Ts; % Time vector

    subplot(211); stem(t(1:40),data(1:40),'^r','filled');grid;

    subplot(212); stairs(t(1:40),data(1:40),'k');grid;

    %% Example 7 [Time domain for Digital signals]

    clear all;close all;clc;

    Ts=0.1; % Sampling Period is 0.1 s

    N=120; % Number of bits are 120 bitst=(0:1:N-1)*Ts; % Time vector

    data=randi([0 1],N,1);

    subplot(211); stem(t,data);grid;

    subplot(212); stairs(t,data);grid;

    6

  • 8/2/2019 Matlab Signal Representation Examples

    7/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia

    %% Example 8 [Time domain for Digital signals]

    clear all;close all;clc;

    Fs=8000; % Sampling rate

    N=512; % Number of bits are 512 symbols

    M=16; % Alphabit size

    data=randi([0 M-1],N,1); %Digital Symbols

    t=(0:0.01*Fs)'/Fs; % Time vector

    stem(t(1:10),data(1:10));grid;

    %

    % In this example, the plot was for only 10 ms. We saw the sampling

    % frequency Fs=8000 Hz, this means Ts=1/Fs=0.125 ms. Hence; in each 1.25 m

    % there are 10 symbols.

    7

  • 8/2/2019 Matlab Signal Representation Examples

    8/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia

    8

  • 8/2/2019 Matlab Signal Representation Examples

    9/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia

    %% Example 9 [Frequency domain for Digital signals]

    clear all;close all;clc;

    % A sinusoidal signal of frequency = 1 kHz, sampled at Fs=8 kHz.

    % This signal must be plotted in time domain and frequency domain both in% one-sided and two sided for 1000 points.

    Fs=8e3; % sampling frequency

    N=1000; % number of data points

    Fm=1e3; % signal frequency

    t=[0:1:N-1]'/Fs; % time index

    x=sin(2*pi*Fm*t);

    figure(1);

    plot(t(1:N/10),x(1:N/10));grid

    X=abs(fft(x))/N;

    P=X.*X;

    f=[0:1:N-1]*Fs/N;figure(2);

    subplot(211);plot(f,X);grid;

    xlabel('Frequency in Hz');

    subplot(212);plot(f,P);grid;

    xlabel('Frequency in Hz');

    %% converting to one side

    X(2:N)=2*X(2:N);

    P=X.*X;

    f=[0:1:N/2]*Fs/N;

    figure(3);

    subplot(211);plot(f,X(1:N/2+1));grid;

    xlabel('Frequency in Hz');

    subplot(212);plot(f,P(1:N/2+1));grid;

    xlabel('Frequency in Hz');

    % For those working with OFDM system, if you want to find the

    % Peak to Average Power Ratio of the signal:

    L=length(X);

    p=max(P);

    m=sum(abs(X.^2))/L;

    paprvalue=10*log10(p/m);

    msgbox(num2str(paprvalue),'PAPR')

    figure(4);

    a=real(X);b=imag(X);

    subplot(411);stem(a);

    subplot(412);stem(b);

    c=abs(X);

    d=angle(X);

    subplot(413);stem(c);

    subplot(414);stem(d);

    9

  • 8/2/2019 Matlab Signal Representation Examples

    10/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia

    10

  • 8/2/2019 Matlab Signal Representation Examples

    11/11

    Montadar Abas Taher Matlab Examples for Signal Representation Prof. Mahamod Ismail

    P54852 Department of Electrical, Electronic & Systems EngineeringFaculty of Engineering & Built Environment

    National University of Malaysia

    11