sf = 44100; % Sampling frequency
d = .25; % Duration in seconds
f = 1000; % frequency in hertz
I = 1; % starting intensity
% tone synthesis
t = linspace(0, d, sf*d);
tone = sin(2*pi*f*t);
% play the sound the first time and get the answer
sound(tone, sf);
answer = input('Can you hear the tone? ');
% repeat the stimulus until the answer changes from "yes" to "no"
while answer == 1
    I = I/2;
    sound(tone * I, sf);
    answer = input('Can you hear the tone? ');
end
% return the threshold
fprintf('The threshold is equal to %2.1f dB Fs\n', 20*log10(I));