%% init
clc;
clear all;
close all;

fvz = 8.82e6;
fs = 44100;


%% read data
input = csvread('am_signal.csv');
input_real = csvread('am_signal_test.csv');
carier = csvread('carier.csv');
modulate = csvread('modulate.csv');
t = (0:(1/fvz):(length(input)/fvz)-1/fvz);
t_real = (0:(1/fvz):(length(input_real)/fvz)-1/fvz);

%% demodulate harmonic signal
output = input.*carier;
demod = filter(lp,output);
demod = filter(highp,demod);
figure(5)
subplot(3,1,1);
plot(t(1000:3000),input(1000:3000));
subplot(3,1,2);
plot(t(1000:3000),demod(1000:3000));
subplot(3,1,3);
plot(t(1000:3000),modulate(1000:3000));

%% demodulate real signal
output_real = input_real .* carier;
demod_real = downsample(output_real ,200);
demod_real = filter(lp,demod_real);
demod_real = filter(highp,demod_real);
demod_real = demod_real * (-1);
modulate = downsample(modulate,200);

ts = (0:1/fs:(length(demod_real)/fs - 1/fs));

figure(6)
subplot(3,1,1);
plot(ts,modulate);
subplot(3,1,2);
plot(t_real,output_real);
subplot(3,1,3);
plot(ts,demod_real);

figure(7);
Sdemodam = (abs(fft(demod_real)));
frekv = (0:(fs/length(ts)):(fs-fs/length(ts)));
plot (frekv,Sdemodam);
figure(8);
Sdemod = (abs(fft(demod)));
frekv = (0:(fvz/length(t)):(fvz-fvz/length(t)));
plot (frekv,Sdemod);

%wavwrite(demod_real,fs,'demod_am.wav');

