%% init
clc;
clear all;
close all;

fvz = 8.82e6;
fs = 44100;
fc = 1e6;
Uc = 0.5;
faux = 1500;
fm = 2e3;
Um = 0.2;

testssb = wavread('audio_test.wav');
kanalssb = testssb(1:20000);
kanalssb = filter(band_audio,kanalssb);
%kanalssb = interp(kanalssb(1:40000,1),200);
clear testssb;
%% modulate harmonic
faux = 1500;
tharm = (0:(1/fvz):((2000000/fvz)-1/fvz));
tharm2 = (0:(1/fs):((10000/fs)-1/fs));
carier = Uc* cos(2*pi*(fc+faux)*tharm);
cariersin = Uc* sin(2*pi*(fc+faux)*tharm);

carier_aux =  Uc* cos(2*pi*faux*tharm2);
carier_aux_sin =  Uc* sin(2*pi*faux*tharm2);


modulating = Um*cos(2*pi*fm*tharm2);

firstbranch = modulating .* carier_aux;
secondbranch = modulating .* carier_aux_sin;

firstbranch2 = filter(hp_ssb,firstbranch);
secondbranch2 = filter(hp_ssb,secondbranch);

firstbranch3 = interp(firstbranch2,200);
secondbranch3 = interp(secondbranch2,200);

firstout = firstbranch3 .* carier;
secondout = secondbranch3 .* cariersin;

ssbharm = firstout + secondout;

figure(1);
subplot(3,1,1);
plot (tharm,carier);
subplot(3,1,2);
plot (tharm2,modulating);
subplot(3,1,3);
plot (tharm,ssbharm);

figure(2);
Susbharm = (abs(fft(ssbharm)));
frekv = (0:(fvz/length(tharm)):(fvz-fvz/length(tharm)));
plot (frekv,Susbharm);


%% modulate real

taux = (0:(1/fs):(length(kanalssb)/fs)-1/fs);


carier_auxr =  Uc* cos(2*pi*faux*taux);
carier_aux_sinr =  Uc* sin(2*pi*faux*taux);


firstbranchr = kanalssb .* carier_auxr';
secondbranchr = kanalssb .* carier_aux_sinr';

firstbranchr2 = filter(hp_ssb,firstbranchr);
secondbranchr2 = filter(hp_ssb,secondbranchr);

firstbranchr3 = interp(firstbranchr2,200);
secondbranchr3 = interp(secondbranchr2,200);


t = (0:(1/fvz):(length(firstbranchr3)/fvz)-1/fvz);
carier_ssb = Uc* cos(2*pi*(fc+faux)*t);
carier_ssbsin = Uc* sin(2*pi*(fc+faux)*t);

firstoutr = firstbranchr3 .* carier_ssb';
secondoutr = secondbranchr3 .* carier_ssbsin';

ssb_real =firstoutr + secondoutr;

%% plots 
figure(3)
skanal = (abs(fft(firstbranchr)));
frekv = (0:(fs/length(skanal)):(fs-fs/length(skanal)));
plot (frekv,skanal);

figure(4)
sfirst = (abs(fft(firstbranchr2)));
frekv = (0:(fs/length(sfirst)):(fs-fs/length(sfirst)));
plot (frekv,sfirst);

figure(5);
Susb = (abs(fft(ssb_real)));
frekv = (0:(fvz/length(Susb)):(fvz-fvz/length(Susb)));
plot (frekv,Susb);

figure(6);
subplot(2,1,1);
plot (taux,kanalssb);
subplot(2,1,2);
plot (t,ssb_real);


%% write data
csvwrite('ssb_signal_lsb.csv',ssb_real);
csvwrite('ssb_harm_lsb.csv',ssbharm);
csvwrite('ssbmodulate.csv',kanalssb);
csvwrite('ssbcarier.csv',carier_ssb);