Merge branch 'featureSpectAnalysis' into develop

This commit is contained in:
Loic Delattre 2023-03-24 12:01:38 +01:00
commit c6cd7ca002
8 changed files with 119 additions and 0 deletions

21
FT_times.m Normal file
View File

@ -0,0 +1,21 @@
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('Standard deviation of DFT time: %d \n', std(times(1:item_num, 1)))
printf('Average FFT time: %d \n', mean(times(1:item_num, 2)))
printf('Standard deviation of FFT time: %d \n', std(times(1:item_num, 2)))

View File

@ -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

BIN
one.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

46
spectral_analysis.m Normal file
View File

@ -0,0 +1,46 @@
clear all
close all
clc
[y, fs] = audioread("sound/modulator22.wav");
ranges = [17000, 20000; 29000, 37000; 41000, 46000];
one = y(ranges(1,1):ranges(1,2));
two = y(ranges(2,1):ranges(2,2));
three = y(ranges(3,1):ranges(3,2));
word = one;
n = length(word);
f = (0:n-1)*(fs/n);
f1 = 0;%Hz
f2 = 4000;%Hz
idx = find(f >= f1 & f <= f2); %define the index of the freq range
f = f(idx);
y = fft(word, n);% compute DFT of input signal
power = abs(y).^2/n;
power = power(idx);
[val, ind] = max(power);
%lowpass for the formant
Fc = 2000; % define the cutoff frequency of the low-pass filter
[b, a] = butter(6, Fc/(fs/2), 'low'); % design a 4th-order Butterworth low-pass filter
Pxx_filt = filter(b, a, power); % apply the filter to the power spectrum
length(Pxx_filt)
length(f)
figure;
subplot(1,2,1) % time plot
plot(0:1/fs:(length(word)-1)/fs,word);
xlabel('Time (s)');
ylabel('Amplitude (a.u.)');
subplot(1,2,2) % freq range plot
plot(f,10*log10(power/power(ind))); hold on;
plot(f, 10*log10(Pxx_filt), 'r');
xlabel('Frequency (Hz)')
ylabel('Power (dB)')

8
spectrogram_analysis.m Normal file
View File

@ -0,0 +1,8 @@
clear all
close all
clc
[y, fs] = audioread("sound/modulator22.wav");
step_size = 5; %ms
window_size = 30;%ms, ideal value 25
spectrogram(y, fs, step_size, window_size)

BIN
three.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

BIN
two.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB