AM and DSB-SC Modulation and Demodulation of a Periodic Square Wave (Matlab)

We once had a homework way back when decided to share it. I edited this script which is in Matlab. I hope you like it. It simply is an analysis of AM and DSB-SC Modulation and Demodulation of a Periodic Square Wave.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%Amplitude Modulation with a periodic sqare wave and its spectrum analysis
%Show the time domain and frquency domain representation of DSB-AM and
%DSB-SC modulations
%By : John Roach – 6 March 2009
%visit my site at http://johnroach.info
N = 1024; %N point FFT N>fc to avoid freq domain aliasing
fs = 4096; % Sample frequency
t = (0:N-1)/fs;
fc = 600; %Carrier Frequency
fm2 = 80; %Message Frequency
Ec = 20; %Carrier Amplitude
Em2 = 5; %Messagae Amplitude
% Try changing the message and carrier amplitudes to see the effect in
% DSB-AM modulation
%———Double SideBand Full Carrier Modulation (DSB-FC(AM))
A = Ec + Em2*square(2*pi*fm2*t);%Envelope/eliminate the carrier amplitude
m = A.*sin(2*pi*fc*t); %to convert DSB-AM to DSB-SC
Mf = 2/N*abs(fft(m,N));
f = fs * (0 : N/2) / N;%Since the fft result is symmetrical, only the
%positive half is sufficient for spectral representation
close all;
figure(’Name’,'Time/Fequency domain representations of DSB-AM signals’);
subplot(2,1,1); %Time domain plot
plot(t(1:N/2),m(1:N/2),t(1:N/2),A(1:N/2),’r',t(1:N/2),-A(1:N/2),’r');
title(’Time Domain Representation’);
xlabel(’Time’); ylabel(’Modulated signal’);
subplot(2,1,2); %Frequency Domain Plot
plot(f(1:256),Mf(1:256));
title(’Frequency Domain Representation’);
xlabel(’Frequency (Hz)); ylabel(’Spectral Magnitude’);
%———-Double SideBand Suppressed Carrier DSB-SC———-
A = Em2*square(2*pi*fm2*t) ; %Envelope/eliminate the carrier amplitude
m = A.*sin(2*pi*fc*t); %to convert DSB-AM to DSB-SC
Mf = 2/N*abs(fft(m,N));
figure(’Name’,'Time/Fequency domain representations of DSB-SC signals’);
subplot(2,1,1); %Time domain plot
plot(t(1:N/2),m(1:N/2),t(1:N/2),A(1:N/2),’r',t(1:N/2),-A(1:N/2),’r');
title(’Time Domain Representation’);
xlabel(’Time’); ylabel(’Modulated signal’);
subplot(2,1,2); %Frequency Domain Plot
plot(f(1:256),Mf(1:256));
title(’Frequency Domain Representation’);
xlabel(’Frequency (Hz)); ylabel(’Spectral Magnitude’);
text(15,60,’Carrier’);
%——————————————————————–
Fork me on GitHub

Related posts:

  1. Amplitude Modulation of a cosine message
  2. Plotting and finding the magnitude of a wav file in MATLAB[edited]
  3. Sampling of a Sinusoidal Signal and finding the DTFT [edited]
  4. Mid-rise type quantizer

21 thoughts on “AM and DSB-SC Modulation and Demodulation of a Periodic Square Wave (Matlab)

  1. write a matlab program that show double side band suppress carrier AM (dsbsc-AM) with carrier frequency fc=100 hz and modulating frequency fm=2hz. than plot AM signal vs time and spectrum vs frequency.

    help me to give the solution of this exercise.
    thnks bro

    • Below you can find the answer to your problem just edit as you wish.
      fc is your carrier freq while your fm is your message freq
      The below example has both modulation and demodulation
      of the message. Remove the comments to see only the modulation of the message.
      If you have any questions feel free to ask from here.

      Good Luck

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      
      function DSB_SC
       
      N=2000;
      F=(-N/2:N/2-1)/N;
       
      fc=0.25;
      Ac=1;
       
      n=0:1:256;
       
      c=Ac*cos(2*pi*n*fc);
       
      %subplot(2,2,1)
      %plot(F, fftshift(abs(fft(c,N))));
      %title('Carrier Wave at frequency-domain');
      %xlabel('frequency'), ylabel('amplitude');
      %grid on;
       
      fm=0.01;
      Am=1;
      m=Am*cos(2*pi*fm*n);
      %subplot(2,2,2)
      %plot(F, fftshift(abs(fft(m,N))));
      %title('Message at frequency-domain');
      %xlabel('frequency'), ylabel('amplitude');
      %grid on;
       
      s=m.*c;
      % plot(F, fftshift(abs(fft(s,N))));
      % title('s(n)');
      % xlabel('frequency'), ylabel('amplitude');
      % grid on;
       
      fcl=0.27;
      x=s.*(cos(2*pi*fcl*n));
      % 
      % plot(F, fftshift(abs(fft(x,N))));
      % title('x(n) = s(n) cl(n)');
      % xlabel('frequency'), ylabel('amplitude');
      % grid on;
       
      b = fir1(8,0.3);
      w = conv(x,b);
      plot(F, fftshift(abs(fft(w,N))));
      title('v(n)');
      xlabel('frequency'), ylabel('amplitude');
      grid on;
    • hello jack.

      Do you get the code for your assignment? Because i also search for that assignment. If you get plz help me.

  2. Can you write a code that does the DSB-SC Modulation and demodulation of a pre-recored .wav file with sample frequency 11025 to play in two channels centered at 20 KHz and
    40 KHz? Thanks,

    • Dear Futja,

      I thank you for answering to my post. It seems this subject “AM and DSB-SC Modulation” is so far the favorite post among my readers. I will keep this in mind in future postings.

      The code you want will mostly be the same however you will need to read the wav file through MATLAB. For this you will need to use ;

      y = wavread(filename)

      After that things go fun. You can try to simply copy and paste my DSB-SC code. However it may not work. I really don’t know since I never really worked with wav files. However I should try.

      If you were waiting for a full answer I am quite sorry but TANSTAAFL.

  3. Hey i just need ur help here!!

    what if the message signal [ m(t) ] is given as in:
    t for 0.1=<t<1
    -t+2 for 1=<t<1.9
    0.1 for elsewhere

    and carrier is normal with fc=50 and Ac=1

    how do i plot it in matlab for message signal??? plssss help me!

    • You will need to create a simple function that accommodates the message signal parameters. If I were you I would have simply created an array. And simply enter that array as the function.

  4. Hello sir, please give me MATLAB code for SSB with carrier and without carrier from DSB using filter please give frequency domain plots

    • I can’t do your homework for you. You have to work that out. I can give you some advice though… Read Simon Haykin’s telecommunication book and also Oppenheim’s DSP book.

  5. Hello,
    i need your help…

    if the question,
    m(t) = 2cos(200πt) + 3cos(300πt) – 5sin(80πt) modulates the carrier c(t) =
    8cos(1000πt) to produce a signal s(t), given the sampling interval ts = 0.0001, and
    time domain for 0 ≤ t ≤ t , where t0 = 0.1. how do i plot the message and carrier signal? Thanks for your suggestion

    • Hi there Nurash,

      I’m really sorry but it’s been more than a year. And I really don’t remember in detail how to do homework.
      Please follow the following steps:
      1- Remember that matlab will only plot what you give it.
      2- Please read the Simon Haykin Telecommunication (book edition 2 or 4) chapters involving AM, DSB-SC, FM, Quantization
      3- Please read the Oppenheim DSP (book edition ‘whatever’) chapters involving sampling and sampling theory
      4- Once you have read these chapters create a flow chart on how the signal will need to be processed.
      5- Sample the signal which needs to be sampled
      6- Please post your answer here so others may us it too…

      Have a nice day sorry I couldn’t help.

  6. hi, i looked at your code and it looks great. but i’m working on a code that uses square wave as a message signal at 1Mhz and carrier of 5Mhz. i’m using product(coherent) demodulation technique for demodulation using butterworth filter. my message amplitude is 0.25v and it is a combination of a dc of amplitude 3.5v and periodic square at 0.25v. but i can get my square wave at the output? can u help me with this?
    I_Rx=(I_mod).*(I_ch);
    Fc=4e6;
    Fs=32e6;
    %f = [0 0.25 0.25 1]; m = [1 1 0 0];
    wn=(2.*Fc)./Fs;disp(wn);
    %h=butter_lpf;
    [b,a]=butter(8, wn);
    z=filter(b,a,I_Rx);
    figure; plot(t,z);

    Here I_Rx is my product signal,I_mod is my modulated signal,I_ch is the carrier i’m multiplying for the product demodulation but i cant low pass filter it to get back my square wave?

  7. thats what i cant figure out….plz help….may be i think it is because my message is a combination of an ac and a dc signal…..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">