From 1fdcf8dfa3c7d9e8e88f2bab7c2a830bdd467bbb Mon Sep 17 00:00:00 2001 From: Loic Delattre Date: Fri, 24 Mar 2023 09:44:27 +0100 Subject: [PATCH] spectrogram and FT times test --- FT_times.m | 19 ++++++++++++++++ frequencySpectrum_noplot.m | 44 +++++++++++++++++++++++++++++++++++++ sound/output.wav | Bin 98312 -> 98312 bytes spectral_analysis.m | 9 ++++++++ 4 files changed, 72 insertions(+) create mode 100644 FT_times.m create mode 100644 frequencySpectrum_noplot.m create mode 100644 spectral_analysis.m diff --git a/FT_times.m b/FT_times.m new file mode 100644 index 0000000..d18fccf --- /dev/null +++ b/FT_times.m @@ -0,0 +1,19 @@ +clear all +close all +clc + +[y, fs] = audioread("sound/modulator22.wav"); +times = []; +item_num = 20; +for j = 1:item_num + localtime = []; + for i = 0:1 + t0 = clock (); + frequencySpectrum_noplot(y, fs, 0); %true does FFT, false DFT + localtime(i+1) = etime (clock (), t0); + endfor + times = [times; localtime]; +endfor + +printf('Average DFT time: %d \n', mean(times(1:item_num, 1))) +printf('Average FFT time: %d \n', mean(times(1:item_num, 2))) \ No newline at end of file diff --git a/frequencySpectrum_noplot.m b/frequencySpectrum_noplot.m new file mode 100644 index 0000000..fc4cd51 --- /dev/null +++ b/frequencySpectrum_noplot.m @@ -0,0 +1,44 @@ +function [power, duration] = frequencySpectrum_noplot(signal, fs, pad) +%%%%%%%%%%%%%%%%%% +%function power = frequencySpectrum(signal, fs, pad) +% +% Task: Display the power spectrum (lin and log scale) of a given signal +% +% Input: +% - signal: the input signal to process +% - fs: the sampling rate +% -pad: boolean if true, signal is padded with 0 to the next power of 2 -> FFT instead of DFT +% +% Output: +% - power: the power spectrum +% +% +% Guillaume Gibert, guillaume.gibert@ecam.fr +% 25/04/2022 +%%%%%%%%%%%%%%%%%% + +n = length(signal); % number of samples + +if (pad) + n = 2^nextpow2(n); +end + +tic +y = fft(signal, n);% compute DFT of input signal +duration = toc; + +power = abs(y).^2/n; % power of the DFT + +[val, ind] = max(power); % find the mx value of DFT and its index + +t=0:1/fs:(n-1)/fs; % time range +%pad signal with zeros +if (pad) + signal = [ signal; zeros( n-length(signal), 1)]; +end + +f = (0:n-1)*(fs/n); % frequency range + + + + diff --git a/sound/output.wav b/sound/output.wav index e1ea9637bfb0a07dac2763890515ea69e66561c4..b00d743181d8c9e85274adf97c7e61c0bb90b132 100644 GIT binary patch delta 23 ecmeBZVC!gLn;^k1sLjBj6vogfzg3>`Wjz2%;|5&- delta 23 ecmeBZVC!gLn;^mNvW9`7BZ9F}eycp=%X$D;PzQPd diff --git a/spectral_analysis.m b/spectral_analysis.m new file mode 100644 index 0000000..c79bb80 --- /dev/null +++ b/spectral_analysis.m @@ -0,0 +1,9 @@ +clear all +close all +clc + +[y, fs] = audioread("sound/modulator22.wav"); +%frequencySpectrum(y, fs, 0); %true does FFT, false DFT +step_size = 5; %ms +window_size = 30;%ms +spectrogram(y, fs, step_size, window_size) \ No newline at end of file