%% init
clc;
clear all;
close all;

fvz = 8.82e6;
fs = 44100;

%% read data
input = csvread('dsb.csv');
input_real = csvread('dsb_test.csv');
carier = csvread('carier.csv');
modulating = csvread('modulate_dsb.csv');
t = (0:(1/fvz):(length(input)/fvz)-1/fvz);
t_real = (0:(1/fvz):(length(input_real)/fvz)-1/fvz);

%% demodulate harmonic
output = input.*carier;
demod = filter(lp,output);
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),modulating(1000:3000));
figure(6)
Sdsbdem = (abs(fft(demod)));
frekv = (0:(fvz/length(demod)):(fvz-fvz/length(demod)));
plot (frekv,Sdsbdem);

%% demodulate real
output_real = input_real.*carier;
demod_real = downsample (output_real,200);
demod_real = filter(lp,demod_real);

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

figure(7)
subplot(3,1,1);
plot(t,input_real);
subplot(3,1,3);
plot(t,output_real);
subplot(3,1,2);
plot(ts,demod_real);

figure(8);
Sdsbdemod = (abs(fft(demod_real)));
frekv = (0:(fs/length(ts)):(fs-fs/length(ts)));
plot (frekv,Sdsbdemod);

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

