%Project One Demo EE451/551
clear all; close all;

Fs_voice=22050; %sampling rate 22.05KHz
Channel_index=0; %channel from 0 to 8;
Fc0=150000; %Fc of channel 0 is 150KHz
Bandwidth_AM=30000; %Bandwidth for each AM channel is 30KHz
Bandwidth_SSB=15000; %Bandwidth for each AM channel is 15KHz
Bandwidth_FM=200000; %Bandwidth for each FM channel is 200KHz

%sample frequency for modulated data Fs>=2*(Fc0+1*Bandwidth)
Fs_AM=2*(Fc0+1*Bandwidth_AM);
Fs_SSB=2*(Fc0+2*Bandwidth_SSB);
Fs_FM=2*(Fc0+1*Bandwidth_FM);

%Please notice that when you record the sound, adjust the rate to 22.05KHz
%in your recorder.
input_wave=wavread('my_name_Zhu_Han'); %read the file from the data. 

%normalize the input file to 1.5 second
[size_input tmp]=size(input_wave);
input_normalize_length=zeros(Fs_voice*1.5,1);
if (size_input>=Fs_voice*1.5)
    input_normalize_length=input_wave(1:1:Fs_voice*1.5);
else
    input_normalize_length(1:1:size_input)=input_wave;
end
%normalize the amplitude to have the maximal of 1 and remove DC
maximal_value_input=(max(abs(input_normalize_length)));
average_DC=sum(input_normalize_length)/(Fs_voice*1.5);
mu=1; % 100% AM 
data_for_mod=mu*(input_normalize_length-average_DC)/maximal_value_input;

%AM modulation
A=1; 
%notice the resample for matlab function ammod
Am_modulated_data = ammod(resample(data_for_mod,Fs_AM,Fs_voice), Fc0+Channel_index*Bandwidth_AM, Fs_AM, [], A);

%add another channel
%Am_modulated_data2 = ammod(resample(data_for_mod,Fs_AM,Fs_voice), Fc0+(Channel_index+1)*Bandwidth_AM, Fs_AM, [], A);
Am_modulated_data = (Am_modulated_data+Am_modulated_data);
%add noise
Noise_level=0.01;
[size_channel tmp]=size(Am_modulated_data);
Am_modulated_data=Am_modulated_data+Noise_level*randn(size_channel,1);


%demodulation
Am_demodulated_data= resample(amdemod(Am_modulated_data,Fc0+Channel_index*Bandwidth_AM,Fs_AM,[],A),Fs_voice,Fs_AM);

%denormalize and ad DC
output_data_AM=Am_demodulated_data*maximal_value_input+average_DC;

%play voice
disp('AM')
sound(output_data_AM,Fs_voice)

pause(1.5)

%SSB modulation
 
%notice the resample for matlab function ammod
SSB_modulated_data = ssbmod(resample(data_for_mod,Fs_SSB,Fs_voice), Fc0+Channel_index*Bandwidth_SSB, Fs_SSB);

%add another channel
SSB_modulated_data2 = ssbmod(resample(data_for_mod,Fs_SSB,Fs_voice), Fc0+(Channel_index+1)*Bandwidth_SSB, Fs_SSB);
SSB_modulated_data = (SSB_modulated_data+SSB_modulated_data2);
%add noise
Noise_level=0.01;
[size_channel tmp]=size(SSB_modulated_data);
SSB_modulated_data=SSB_modulated_data+Noise_level*randn(size_channel,1);

%demodulation
SSB_demodulated_data= resample(ssbdemod(SSB_modulated_data,Fc0+Channel_index*Bandwidth_SSB,Fs_SSB),Fs_voice,Fs_SSB);

%denormalize and ad DC
output_data_SSB=SSB_demodulated_data*maximal_value_input+average_DC;

%play voice
disp('SSB')
sound(output_data_SSB,Fs_voice)

pause(1.5)


%FM modulation
beta=5;
frequency_div=Fs_voice/2*beta; 
%notice the resample for matlab function ammod
Fm_modulated_data = fmmod(resample(data_for_mod,Fs_FM,Fs_voice), Fc0+Channel_index*Bandwidth_FM, Fs_FM, frequency_div);

%add another channel
%Fm_modulated_data2 = fmmod(resample(data_for_mod,Fs_FM,Fs_voice), Fc0+(Channel_index+1)*Bandwidth_FM, Fs_FM, frequency_div);
%Fm_modulated_data = (Fm_modulated_data+Fm_modulated_data2);
%add noise
Noise_level=0.1;
[size_channel tmp]=size(Fm_modulated_data);
Fm_modulated_data=Fm_modulated_data+Noise_level*randn(size_channel,1);

%filter design to filter the desired channel.

%demodulation
Fm_demodulated_data= resample(fmdemod(Fm_modulated_data,Fc0+Channel_index*Bandwidth_FM,Fs_FM, frequency_div),Fs_voice,Fs_FM);

%denormalize and ad DC
output_data_FM=Fm_demodulated_data*maximal_value_input+average_DC;

%play voice
disp('FM')
sound(output_data_FM,Fs_voice)

pause(1.5)