Bonus Problem

close all;
clear all;

Fs = 44100;

% load a 2kHz noise with bw of 100 Hz generated previously from Simulink
load twok_noise100;

two_k_noise = two_k_noise/max(abs(two_k_noise));

fc = 2000;  % center frequency

t = 0:1/Fs:2-1/Fs;


%initialize data
playThis = zeros(1,2*Fs);
data_points = 0;


%outer loop changes tone separation
%inner loop changes noise level

for tonesep = [200 250 300 400 500]
    toneL = sin(2*pi*(fc-tonesep/2)*t);
    toneR = sin(2*pi*(fc+tonesep/2)*t);

    for noiseLevel = [0 .7:-.1:0 .001]
        % add the two tones together with the noise
        playThis = toneL+toneR+noiseLevel*two_k_noise';

        % soundsc is not used because we are very concerned about level.
        % instead, we divide 'playThis' by three, beause all signals are
        % initially between -1 and 1
        sound(playThis/3,Fs);

        % get the user feedback
        canHear = input('Can you hear the center noise?     ','s');
        if (canHear == 'n')
            %noise was masked, store current noiseLevel
            data_points(length(data_points)+1) = noiseLevel;
            break;
        else
            %noise was not masked, store .001 if it was never masked
            if(noiseLevel == .001)
               data_points(length(data_points)+1) = noiseLevel;
            end
        end
    end
end

% get rid of first data point (it was pre-allocated as zero)
data_points = data_points(2:length(data_points));

% visualize data and look for 'knee' of graph to determine critical band
tonesep = [200 250 300 400 500];
stem(tonesep,data_points);
grid on;