الثلاثاء، 17 ديسمبر 2013

doing fm modulation and demodulation matlab

clear all;
close all;
clc;

%% Frequency MODULATION of Triangular Signal
t = -0.04:1.e-4:0.04;
Ta = 0.01;
fc = 200;                                                           % Frequency of carrier
msg = msg1(t,Ta);                                            % Calling Function to Generate Msg Signal

%% MODULATION
kf = 160*pi;
m_int = kf*1.e-4*cumsum(msg);              % Integrating Msg
fm = cos(2*fc*pi*t + m_int);                   % fm = cos(2*pi*fc*t + integral(msg))

%% DEMODULATION (Using Differentiator)
dem = diff(fm);                
dem = [0,dem];
rect_dem = abs(dem);

%% Filtering out High Frequencies
N = 80;                                                             % Order of Filter
Wn = 1.e-2;                                                    % Pass Band Edge Frequency.
a = fir1(N,Wn);                                             % Return Numerator of Low Pass FIR filter
b = 1;                                                                % Denominator of Low Pass FIR Filter
rec = filter(a,b,rect_dem);

%% Finding frequency Response of Signals

fl = length(t);
fl = 2^ceil(log2(fl));
f = (-fl/2:fl/2-1)/(fl*1.e-4);

mF = fftshift(fft(msg,fl));               % Frequency Response of Message Signal
                                       
fmF = fftshift(fft(fm,fl));             % Frequency Response of FM Signal

rect_demF = fftshift(fft(rect_dem,fl)); % Frequency Response of Rectified FM Signal

recF = fftshift(fft(rec,fl));           % Frequency Response of Recovered Message Signal


%% Plotting signal in time domain
figure;
subplot(2,1,1);
plot(t,msg);
title('Message Signal');
xlabel('{\it t} (sec)');
ylabel('m(t)');
grid;

subplot(2,1,2);
plot(t,fm);
title('FM');
xlabel('{\it t} (sec)');
ylabel('FM');
grid;

figure;
subplot(2,1,1);
plot(t,rect_dem);
title('Rectified FM');
xlabel('{\it t} (sec)');
ylabel('dem')
grid;

subplot(2,1,2);
plot(t,rec);
title('Recovered Signal');
xlabel('{\it t} (sec)');
ylabel('m(t)');
grid;

%% Plotting Freq Response of Signals
figure;
subplot(2,2,1);
plot(f,abs(mF));
title('Freq Response of MSG');
xlabel('f(Hz)');
ylabel('M(f)');
grid;
axis([-600 600 0 200]);

subplot(2,2,2);
plot(f,abs(fmF));
title('Freq Response of FM');
grid;
xlabel('f(Hz)');
ylabel('C(f)');
axis([-600 600 0 300]);

subplot(2,2,3);
plot(f,abs(rect_demF));
title('Freq Response of Rectified FM');
xlabel('f(Hz)');
ylabel('DSBSC(f)');
grid;
axis([-600 600 0 100]);

subplot(2,2,4);
plot(f,abs(recF));
title('Freq Response of Recovered Signal');
xlabel('f(Hz)');
ylabel('M(f)');
grid;
axis([-600 600 0 100]);

fm demodulation matlab

clc
clear
close all

Fs = 8000; % Sampling rate of signal
Fc = 3000; % Carrier frequency
t = [0:Fs]/Fs; % Sampling times
s1 = sin(2*pi*10*t); % Channel 1
%s2 = sin(2*pi*150*t)+2*sin(2*pi*900*t); % Channel 2
%x = [s1]; % Two-channel signal
dev = 50; % Frequency deviation in modulated signal
y = fmmod(s1,Fc,Fs,dev); % Modulate both channels.
z = fmdemod(y,Fc,Fs,dev); % Demodulate both channels.
subplot(2,1,1); plot(t,y); title('frequency modulated image');% Plot x on top.
subplot(2,1,2); plot(t,z);title('demodulated image');% Plot y below.
الكود ده شغال