%% !!!WARNING!!!!!!WARNING!!!!!!WARNING!!!!!!WARNING!!! TAKE HEADPHONES OFF BEFORE RUNNING THIS!!
%
%% Microhone Check
% Iterates through various frequencies and checks headphone->microphone
% received amplitude.
%
% WARNING!!sEE ABOVE

test_freq = (5:6).*1e3
%test_freq = (0:0.5:24).*1e3;
fs = 48e3;
%test_freq = (24:0.5:48).*1e3;
%fs = 2*48e3;

len = 1;

max_amp = zeros(1, length(test_freq));
fft_data =  cell(1,2) % Cell array, since we don't know how long they are.

mic_check_recorder = audiorecorder(fs, 24, 1);


for i = 1:length(test_freq)
    sine = sin(2*pi*test_freq(i)*(0:1/fs:len));
    mic_check_player = audioplayer(sine, fs); 
    record(mic_check_recorder);
    playblocking(mic_check_player);
    stop(mic_check_recorder)

    
    
    [fft_ret , fAxis] = plot_fft(getaudiodata(mic_check_recorder), fs);
    grid on
    xlim([0 24000])
    ylim([0 0.5])
    fft_data{i,1} = fft_ret;
    fft_data{i,2} = fAxis;
    max_amp(i) = max(fft_data{i});
end

plot(test_freq, max_amp)
title('Headphone->microhone Received Amplitude')
grid on
xlabel('Frequency')
ylabel('fft Amplitude')