am modulation and demodulation matlab

Discussion in 'Programmer's Corner' started by Jon_Snow, Feb 27, 2013.

  1. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    Hey guys I'm trying to build a script to modulate and demodulate an am frequency.

    I have my information frequency, and carrier frequency. I modulate the signal and add noise. After I do that I run it through a halfwave rectifier and low pass and high filter. I then use a convolution to remove the noise. I then write the file to a wave file and I should be able to replay file. At the end of all this I should get something similar to the original frequency, but all I'm getting is static.

    Here's my code.


    close all;
    clear all;
    fs = 50000;
    t = [0:1/fs:5];

    f_cut_off1 = 10000;
    f_cut_off2 = 10;
    f = 2000; %input frequency
    fc = 940e3; %carrier frequency

    k = length(t)


    input_sig = (0.2)*cos(2 * pi * f * t); %input signal

    wavwrite(input_sig,fs,32,'input_sig');

    carrier_sig = (0.5)*cos( 2 * pi * fc * t); %carrier signal

    wavwrite(carrier_sig,fs,32,'carrier_sig')

    mod_sig = input_sig .* carrier_sig + carrier_sig; %modulated signal

    wavwrite(mod_sig,fs,32,'mod_sig')
    %interval to randonize noise
    noise = rand(1, k); %random nosie
    scale = 2;
    new_mod = mod_sig + noise; % mod signal + noise

    wavwrite(new_mod,fs,32,'new_mod')

    subplot(321), plot(t, mod_sig)
    subplot(322), plot(t, new_mod)

    %Apply HW Rectifier
    for i = 1:k
    if new_mod(i) <= 0
    x(i) = 0;
    else
    x(i) = new_mod(i);
    end
    end

    wavwrite(x,fs,32,'hw_rect')

    subplot(323), plot(t, x)
    rc1 = 1 / (2 * pi * f_cut_off1);
    rc2 = 1 / (2 * pi * f_cut_off2);
    %Apply Low Pass Filter
    num2 = [0 1];
    den2 = [rc1 1];
    subplot(324), bode(num2, den2)

    sys2 = tf(num2, den2);

    lpf = lsim(sys2, x, t);




    %Apply High Pass Filter

    num1 = [rc2 0];
    den1 = [rc2 1];

    subplot(325), bode(num1, den1)
    sys1 = tf(num1, den1);

    hpf = lsim(sys1, lpf, t);

    vout = hpf;

    g = 20;
    h = (1 / g) * ones(1, g);

    y = conv(h ,vout);



    wavwrite(y,fs,32,'vout')



    Any ideas? I'm completely stuck.

    Thanks!
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Have you tried plotting your signal at different points?
    raw audio - with noise - after rectification - after filters - after convolution

    That way, you can see what is going on with the waveform...
     
  3. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    I've plotted the original signal, the modulated signal, the signal plotted with noise, and the signal after the halfwave rectifier. Those all seem to be correct.
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Couple of problems:

    1) Your carrier frequency is 940kHz, yet your sampling frequency is only 50kHz.
    Your sampling frequency must be at least twice the carrier frequency.

    2) When you added the noise, the noise value is between 0 and 1. Subtract 0.5 from the noise to make it -0.5 to +0.5
     
  5. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    The size of the sampling frequency was something I was confused by so I'm going to change that, but what would changing the noise from -.5 to .5 do exactly?
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Adding noise from 0 to 1 is like adding a DC offset of 0.5.
    When you do the half-wave rectification you are not going to get the signal chopped in half.
     
  7. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    Ok well I did those 2 things and that definitely helped. I'm still getting static, but at least I can recognize my input signal now.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Remove the noise and see what happens.
     
  9. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Can you post your updated code and state which parts are not working correctly?
     
  10. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    When I remove the noise the static goes away. I'm supposed to put the noise in there however and remove the noise with the convolution. Maybe I should use a higher number than 20 in the convolution?
     
  11. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    Here's my up to date code.

    close all;
    clear all;
    fs = 2e6;
    t = [0:1/fs:5];

    f_cut_off1 = 10000;
    f_cut_off2 = 10;
    f = 2000; %input frequency
    fc = 940e3; %carrier frequency

    k = length(t);


    input_sig = (0.2)*cos(2 * pi * f * t); %input signal

    wavwrite(input_sig,fs,32,'input_sig');

    carrier_sig = (0.5)*cos( 2 * pi * fc * t); %carrier signal



    mod_sig = input_sig .* carrier_sig + carrier_sig; %modulated signal


    %interval to randonize noise
    noise = rand(1, k) - .5; %random nosie

    new_mod = mod_sig + noise; % mod signal + noise


    %Apply HW Rectifier
    for i = 1:k
    if new_mod(i) <= 0
    x(i) = 0;
    else
    x(i) = new_mod(i);
    end
    end



    rc1 = 1 / (2 * pi * f_cut_off1);
    rc2 = 1 / (2 * pi * f_cut_off2);

    %Apply Low Pass Filter
    num2 = [0 1];
    den2 = [rc1 1];
    subplot(324), bode(num2, den2)

    sys2 = tf(num2, den2);

    lpf = lsim(sys2, x, t);




    %Apply High Pass Filter

    num1 = [rc2 0];
    den1 = [rc2 1];

    subplot(325), bode(num1, den1)
    sys1 = tf(num1, den1);

    hpf = lsim(sys1, lpf, t);

    vout = hpf;

    g = 20;
    h = (1 / g) * ones(1, g);

    y = conv(h ,vout);



    wavwrite(y,fs,32,'vout')
     
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    You are only using diode rectification and a single lowpass filter, is this a limitation of your assignment?

    See Section 6 on that handout, it may give some insight.
     
  13. Jon_Snow

    Thread Starter New Member

    Jan 15, 2013
    16
    0
    No it's just a starting a point. I'll check out your link.
     
Loading...