pkg load signal

%load & plot audio file modulator
[audio_data sampling_freq]= audioread('modulator22.wav');

figure;
plot(audio_data);
xlabel('Time in [s]');
ylabel('Amplitude in [dB]');
title('Speech data in time domain');

%FFT vs DFT comparison
if (0)
  t = 5;
  %average
  for i=1:t
    %FFT
    
    [power, duration]=frequencySpectrum(audio_data,sampling_freq,1);
    duration_fft(i) = duration;

    %DFT
    
    [power, duration]=frequencySpectrum(audio_data,sampling_freq,0);
    duration_dft(i) = duration;
   
    sum_fft =0; 
    sum_dft =0;
    
    sum_fft = sum_fft + duration_fft(i);
    sum_dft = sum_fft + duration_dft(i);
  
  endfor

%estimate the duration of FFT
average_fft = sum_fft/t
average_dft = sum_dft/t
end

%Spectrogram
frequencySpectrum(audio_data,sampling_freq,1);

%Compute and display spectrogram
if(0)
  spectrogram(audio_data,sampling_freq,5,5);
  %spectrogram(audio_data,sampling_freq,30,5);
  wan = audio_data(0.37*sampling_freq:1.17*sampling_freq);
  tu = audio_data(1.24*sampling_freq:1.75*sampling_freq);
  tri = audio_data(1.84*sampling_freq:2.19*sampling_freq);

  frequencySpectrum(wan,sampling_freq,1);
  frequencySpectrum(tu,sampling_freq,1);
  frequencySpectrum(tri,sampling_freq,1);
end

%downsampling
audio_downsampled = downsample(audio_data, 2);
audiowrite('downsampled_audio.wav',audio_downsampled ,4000);

%down_deci = decimate(audio_data, 2);
%audiowrite('decimate_audio.wav',down_deci,4000);

%low-pass filter
lp = fir1(30,1000/sampling_freq,'low');
y = filter(lp,1,audio_data);
frequencySpectrum(y,sampling_freq,1);

lp_butter = butter(8,1000/sampling_freq,'low');
y_butter = filter(lp_butter,1,audio_data);
frequencySpectrum(y_butter,sampling_freq,1);