%% Lab1 "Akustik" (VTAN01)
% Juan Negreira
% Division of Engineering Acoustics
% Department of Construction Sciences
% Lunds Tekniska Högskola (LTH), Sweden

% IN THE SAME DIRECTORY YOU ALSO NEED THE FILE fft_function.m and your .wav file

%% Beginning of the assignment
close all, clear all

%% Read the wav file 
% HERE YOU CAN INSERT THE FILENAME OF YOUR AUDIO FILE:
% MAKE SURE THAT THE AUDIO FILE IS IN THE SAME DIRECTORY AS THIS FILE
[y,Fsamp,nbits]=wavread('triangle.wav'); 
%You can convert your signal to .wav for example here:
%http://audio.online-convert.com/convert-to-wav (online wav-converter)
%Note: if the function "wavread" does not work, try the one "audioread"
%instead; it could happen depending on the Matlab version one has.

%% Playing the sound file at the original sample rate.
disp('Playing at the original sample rate.');
sound(y, Fsamp);
%You can "play around" and play the sound at different sampling rates (e.g. Fsamp/2)

%% Plotting the time-signal
tend=length(y)/Fsamp;
t=[0 tend];
t=linspace(0,tend,length(y));

figure(1)
plot(t,y,'r','LineWidth',1,'MarkerSize',12)
set(gca,'FontSize',24)
legend('Time signal')
xlabel('Time [sec]','FontSize',24)
ylabel('Amplitude [V]','FontSize',24)
title('Time Signal of the Sound')
grid on
axis([0 tend -0.9 0.9])

%% Calculus of the signal FFT  
[Y,f]=fft_function(t,y);

%% Plotting the frequency spectrum
figure(2)
plot(f,Y,'b','LineWidth',1,'MarkerSize',12)
set(gca,'FontSize',24)
legend('Signal spectrum')
grid on
axis([0 max(f) 0 0.025])
xlabel('Frequency [Hz]')
ylabel('|Y(f)|')
title('Spectrum of the Original Sound')