Problem 2

clear all
close all


Fs = 44100;
t = 0:1/Fs:5-1/Fs;

signal=chirp(t,20,t(length(t)),20000);
wavwrite(signal,Fs,'chirp');

window_size = 1024;
overlap_percent = .75;
overlap_size = round(window_size*overlap_percent);
overlap_increment = window_size - overlap_size;

result = zeros(1,length(signal)+3*window_size);


% Now lets get our curve
[spl, freqs] = iso226(50);

% do a spline data fit of the curve to linearize the data
xplot1 = linspace(0, 20500,100);
fitResults1 = spline(freqs, spl);
% Evaluate piecewise polynomial
yplot1 = ppval(fitResults1, xplot1);

% resample to our length of choice
half = resample(yplot1,window_size*3/2,100,0);

% duplicate spectrum for the full curve
fullCurve = [half half(length(half)) fliplr(half)];


for r = 5:floor(length(signal)/overlap_increment)-4

    %find start and stop points for the frame
    start_window = r*overlap_increment;
    stop_window = start_window + window_size;

    %grab the frame
    frame = signal(start_window:stop_window);

    %window the frame
    frame = frame.*hamming(length(frame))';

    %zeropad
    frame = [zeros(1,length(frame)-1) frame zeros(1,length(frame)-1)];


    FRAME = fft(frame);

    FRAME = FRAME.*fullCurve;

    %bring things back into the time domain
    new_frame = real(ifft(FRAME));

    %overlap and add
    result(start_window-window_size:stop_window+window_size) = ...
        result(start_window-window_size:stop_window+window_size)+new_frame;


end

result = result/max(abs(result));
wavwrite(result,Fs,'equal_loudness_chirp');

figure(1)
plot(xplot1,yplot1)
title('Equal Loudness Curve derived from official iso226 spec')
xlabel('Frequency (Hz)'),ylabel('Curve Amplitude');
grid on,axis tight;

figure(2)
plot(linspace(0,length(result)/Fs,length(result)),result)
title('Final Sound'),xlabel('Time (sec)'),ylabel('Amplitude');
grid on,axis tight;