% Matt Montag
% EEN 540 Project 1
% Phoneme Analysis

path = '';
words = ...
{ 'Stravinsky' 'Spoken Phrase' };

lf=300;
hf=3500;

for i=1:length(words)
	figure;
	[sig,fs] = wavread([path words{1,i} '.wav']);
	bpf = zeros(1,length(sig));
	f2bin = length(sig)/fs;
	bpf(floor(lf*f2bin):floor(hf*f2bin)) = 1;
	Y = fft(sig);
	Y = Y .* bpf';
	output = real(ifft(Y));
	output = .707 * output / max(abs(output)); % normalize
	
	% magnitude spectrum plots
	Y = fft(sig);
	Y = abs(Y(1:floor(length(Y)/2)));
	dbY = 20*log10(Y);
	dbY = dbY - max(dbY);
	%Y = Y-max(max(Y));
	F = linspace(0,fs/2,length( Y /2))';
	subplot(3,1,2);
	plot( F,dbY); ylabel('dB'); 
	xlabel('Frequency','fontsize',7);
	title([words{1,i} ' - Unfiltered Magnitude Spectrum']);
	
	subplot(3,1,1);
	plot(F,bpf(1:length(F))); title('Telephone Channel Bandpass Filter'); 
	xlabel('Frequency','fontsize',7);
	ylim([-.1 1.1]);
	
	Y = fft(output);
	Y = abs(Y(1:floor(length(Y)/2)));
	dbY = 20*log10(Y);
	dbY = dbY - max(dbY);
	%Y = Y-max(max(Y));

	subplot(3,1,3);
	plot( F,dbY); ylabel('dB'); 
	xlabel('Frequency','fontsize',7);
	title([words{1,i} ' - Filtered Magnitude Spectrum']);

	%sound(output,fs);
	saveas(gcf,words{1,i},'png');
	wavwrite(output,fs,[path words{1,i} '_phone.wav']);
end