pkg load signal

#[signal, Fs] = audioread("modulator22.wav");
#t=0:1/Fs:length(signal)/Fs - 1/Fs;
#figure; % Create a new figure
#plot(t,signal);
#xlabel('Time(s)');
#ylabel('Signal Amplitude (normalized unit)');


#audiowrite("modifiedmodulator.wav",signal,Fs/2);
[signal, Fs] = audioread("Sound/modulator22.wav");
t=0:1/Fs:length(signal)/Fs - 1/Fs;
#player=audioplayer(signal, Fs, 8)
#play(player);
##figure; % Create a new figure
##plot(t,signal);
##xlabel('Time(s)');
##ylabel('Signal Amplitude (normalized unit)');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Parameters for measurements
num_measurements = 100; % Number of measurements
durations_dft = zeros(1, num_measurements);
durations_fft = zeros(1, num_measurements);

for i = 1:num_measurements
    % Measure time taken for DFT
    tic;
    [power_dft, duration_dft] = frequencySpectrum(signal, Fs, false, false, [100 2700]);
    durations_dft(i) = duration_dft;

    % Measure time taken for FFT with padding
    tic;
    [power_fft, duration_fft] = frequencySpectrum(signal, Fs, true, false, [100 2700]);
    durations_fft(i) = duration_fft;
end

% Calculate average and standard deviation
avg_duration_dft = mean(durations_dft);
std_dev_dft = std(durations_dft);

avg_duration_fft = mean(durations_fft);
std_dev_fft = std(durations_fft);

fprintf('Average duration for DFT: %f seconds\n', avg_duration_dft);
fprintf('Standard deviation for DFT: %f seconds\n', std_dev_dft);
fprintf('\n');
fprintf('Average duration for FFT (with padding): %f seconds\n', avg_duration_fft);
fprintf('Standard deviation for FFT (with padding): %f seconds\n', std_dev_fft);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Searching for formants without low pass filter

##start_time = 0.7; % start time in seconds
##end_time = 1.3; % end time in seconds
##
##start_index = round(start_time * Fs) + 1; % start index
##end_index = round(end_time * Fs) + 1; % end index
##
##cropped_signal = signal(start_index:end_index); % cropped signal
##
##[power_dft, duration_dft] = frequencySpectrum(cropped_signal, Fs, false, true, [0 160]);

#spectrogram(signal, Fs, 5, 30);
#spectrogram(signal, Fs, 5, 5);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%formants with low pass filter

N=8;
fc=2700;
[b,a]= butter(N,fc/(Fs/2));
freqz(b,a);

signal_filtered=filter(b,a,signal);

start_time = 0.75; % start time in seconds
end_time = 0.95; % end time in seconds

start_index = round(start_time * Fs) + 1; % start index
end_index = round(end_time * Fs) + 1; % end index

cropped_signal = signal_filtered(start_index:end_index); % cropped signal

[power_dft, duration_dft] = frequencySpectrum(cropped_signal, Fs, false, true, [0 2700]);










