From 802c3166f21c27310e98b7887a77f369838d9c60 Mon Sep 17 00:00:00 2001 From: "adrien.combe" Date: Thu, 20 Apr 2023 11:16:13 +0200 Subject: [PATCH] Signal process 2 --- Midterm-Adrien-COMBE-codes/Signal2.m | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Midterm-Adrien-COMBE-codes/Signal2.m diff --git a/Midterm-Adrien-COMBE-codes/Signal2.m b/Midterm-Adrien-COMBE-codes/Signal2.m new file mode 100644 index 0000000..97a4b0b --- /dev/null +++ b/Midterm-Adrien-COMBE-codes/Signal2.m @@ -0,0 +1,45 @@ +% Load the signal from the CSV file +data = csvread('unknownsignal.csv'); +signal = data(:, 1); + +% Set the sampling frequency +Fs = 300; % Hz + +% Define the frequency range of interest +fmin = 30; % Hz +fmax = 40; % Hz + +% Apply a Hanning window to the signal to reduce spectral leakage +win = hanning(length(signal)); +signal_win = signal .* win; + +% Calculate the power spectral density (PSD) of the signal using Welch's method +nfft = 1024; +[Pxx, f] = pwelch(signal_win, win, [], nfft, Fs); + +% Find the frequency indices that correspond to the frequency range of interest +idx_min = find(f >= fmin, 1); +idx_max = find(f <= fmax, 1, 'last'); + +% Extract the PSD of the signal in the frequency range of interest +Pxx_interest = Pxx(idx_min:idx_max); + +% Find the frequency at which the PSD is maximum in the frequency range of interest +[~, idx_max_interest] = max(Pxx_interest); +f_interest = f(idx_min+idx_max_interest-1); + +% Plot the PSD of the signal and the frequency range of interest +figure; +plot(f, 10*log10(Pxx)); +hold on; +plot([fmin, fmax], [0, 0], 'r', 'LineWidth', 2); +xlim([0, Fs/2]); +ylim([-100, max(10*log10(Pxx))]); +xlabel('Frequency (Hz)'); +ylabel('Power/Frequency (dB/Hz)'); +title(sprintf('Signal PSD with frequency range of interest [%d %d] Hz', fmin, fmax)); +grid on; +legend('Signal PSD', 'Frequency range of interest'); + +% Print the frequency at which the PSD is maximum in the frequency range of interest +fprintf('The signal of interest is at %.2f Hz\n', f_interest);