function y_filtered=filter_application(fprefix, t, y,  g_vs_freq)
% assumes that t is equidistant
dt=mean(diff(t));
SampleRate=1/dt;

N=length(y);

%% defines plot property
lwidth=2;

%% let's plot data
figure(1)
hold off;
niceplotset;
plot(t,y, 'LineWidth', lwidth);
title('Signal vs time');
xlabel('Time (S)');
ylabel('Amplitude');
fname=sprintf('%s_signal.eps', fprefix);
print(fname, '-depsc');

%% let's hear the sound
% sound(y,SampleRate);

%% now we calculate Fourier transform of the data
Y=fft(y);

%% calculate corresponding frequency for the DFT
spectrum_freq=fourier_frequencies(SampleRate, N);

%% plot the spectrum amplitudes 
%
% all spectrums below normilized by N to show realistic signal amplitudes
%
figure(2); hold off;
niceplotset;
% since we have negative frequency at the right we need to find index of sorted freq
[dummy,index]=sort(spectrum_freq);
plot(spectrum_freq(index), abs(Y(index))/N, 'b-', 'LineWidth', lwidth ); % notice abs generaly Y is complex
xlabel('Frequency (Hz)');
ylabel('FFT amplitude');
title('Spectrum amplitudes');
% have a look at this plot and chose proper filter roll off frequency
fname=sprintf('%s_signal_spectrum.eps', fprefix);
print(fname, '-depsc');


spectrum_filtered=Y.*g_vs_freq;

%% filter frequency gain Bode plot
figure(3);
subplot(2,1,1);
niceplotset;
plot( spectrum_freq(index), abs(g_vs_freq(index)), 'r-', 'LineWidth', lwidth );
xlabel('Frequency (Hz)');
ylabel('Filter gain');
title('Filter Bode plot');

subplot(2,1,2);
niceplotset;
plot( spectrum_freq(index), angle(g_vs_freq(index)), 'r-', 'LineWidth', lwidth );
xlabel('Frequency (Hz)');
ylabel('Filter phase');
fname=sprintf('%s_filter_spectrum.eps', fprefix);
print(fname, '-depsc');

%% filtered and unfiltered spectrum
figure(4); hold off;
niceplotset;
plot( spectrum_freq(index), abs(spectrum_filtered(index))/N, 'r-', 'LineWidth', lwidth  ); 
legend('filtered');
title('Signal filtered spectrum');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
fname=sprintf('%s_filtered_spectrum.eps', fprefix);
print(fname, '-depsc');

figure(4); hold off;
niceplotset;
plot( spectrum_freq(index), abs(Y(index))/N, 'b-', 'LineWidth', lwidth  ); hold on;
plot( spectrum_freq(index), abs(spectrum_filtered(index))/N, 'r-', 'LineWidth', lwidth  ); 
legend('raw', 'filtered');
title('Signal spectrum');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
fname=sprintf('%s_filtered_and_unfiltered_spectrum.eps', fprefix);
print(fname, '-depsc');

%% construct filtered signal
y_filtered=ifft(spectrum_filtered);

%% get rid of imaginary part 
y_filtered=real(y_filtered);

%% filtered  signals
figure(5); hold off;
niceplotset;
plot(t, y_filtered,'r-', 'LineWidth', lwidth );
title('Signal vs time');
xlabel('Time (S)');
ylabel('Amplitude');
legend('filtered');
fname=sprintf('%s_filtered_signal.eps', fprefix);
print(fname, '-depsc');

%% output filtered sound
% sound(y_filtered, SampleRate);

%% save the wave file with filtered sound
%wavwrite(y_filtered, SampleRate, NbitsPerSample, 'voice_record_filtered.wav');


%% compare filtered and raw signals
figure(6); hold off;
niceplotset;
plot(t, y, 'b-', 'LineWidth', lwidth ); hold on;
plot(t, y_filtered,'r-', 'LineWidth', lwidth );
title('Signal vs time');
xlabel('Time (S)');
legend('raw', 'filtered');
fname=sprintf('%s_filtered_and_raw_signal.eps', fprefix);
print(fname, '-depsc');

end


