% Experiment with wav files 

[speech, fs]= wavread('sp_truth.wav');
sound(speech, fs)
plot(1:length(speech), speech)
title('Dialog from A Few Good Men')


pause(7)
% Try to find "You can't handle the truth"
disp('Find:  You can''t handle the truth')
pause
sound(speech(50000:end), fs)
figure
plot(50000:length(speech), speech(50000:end))
title('You can''t handle the truth')


pause(3)
% Try to take out "can't"
disp('Edit:  You handle the truth')
pause
lastSen= speech(50000:end);  % last sentence
canHandle= [lastSen(1:3000); lastSen(7700:end)];
sound(canHandle, fs)
figure
plot(1:length(canHandle), canHandle)
title('You handle the truth')


% "the truth"
truth= lastSen(12000:end);

pause(3)
% "Scotty, beam me up"  same fs as afgm
disp('Scotty, beam me up')
pause
[s1, fs1]= wavread('sp_beam.wav');
sound(s1, fs)
figure
plot(1:length(s1), s1)
title('Scotty, beam me up')


pause(3)
close all
% "Scotty, beam me the truth"
disp('Scotty, beam me the truth')
pause
beamTruth= [s1(1:9500); truth];
sound(beamTruth,fs)


pause(4)
% But truth is too loud, make it quieter by decreasing the amplitude
disp('But ''truth'' is too loud, make it quieter by decreasing the amplitude')
pause
quietTruth= zeros(length(truth),1);
for k= 1:length(truth)
    quietTruth(k,1)= truth(k)/2;
end
beamTruth= [s1(1:9500); quietTruth];
sound(beamTruth, fs)


pause(4)
% What if we lose half the data?
% Half the sampling rate of lastSen
lastSenLow= zeros(ceil(length(lastSen)/2), 1);
for k= 1:length(lastSenLow)
    lastSenLow(k)= lastSen(2*k-1);
end
disp('Lose half the data')
pause
sound(lastSenLow, fs/2)  % Half the data
pause(4)
disp('Play original data for comparison')
pause
sound(lastSen, fs)       % Original


pause(4)
% Recovery method 1:  Fill in lost data by repeating previous value
disp('Recovery method 1: Fill in lost data by repeating previous value')
pause
lastSenFill= zeros(length(lastSenLow)*2, 1);
for k= 1:length(lastSenLow)
    lastSenFill(2*k)= lastSenLow(k);
    lastSenFill(2*k-1)= lastSenLow(k);
end
sound(lastSenFill, fs)


pause(4)
% Recovery method 2:  Fill in lost data by interpolating neighboring values
disp('Recovery method 2:  Fill in lost data by interpolation')
pause
lastSenInterpolate= zeros(length(lastSenLow)*2-1, 1);
for k= 1:length(lastSenLow)-1
    lastSenInterpolate(2*k-1)= lastSenLow(k);
    lastSenInterpolate(2*k)= (lastSenLow(k)+lastSenLow(k+1))/2;
end
lastSenInterpolate(length(lastSenLow)*2-1)= lastSenLow(length(lastSenLow));
sound(lastSenInterpolate, fs)


