update with sampling windowing frequencySpectrum code

This commit is contained in:
Dorian VELOSO 2024-03-12 10:21:10 +01:00
parent 113e052293
commit d221ded593
4 changed files with 133 additions and 0 deletions

Binary file not shown.

47
frequencySpectrum.m Normal file
View File

@ -0,0 +1,47 @@
function frequencySpectrum(signal, fs)
%%%%%%%%%%%%%%%%%%
%function frequencySpectrum(signal, fs)
%
% Task: Display the power spectrum of a given signal
%
% Input:
% - signal: the input signal to process
% - fs: the sampling rate
%
% Output: None
%
%
% Guillaume Gibert, guillaume.gibert@ecam.fr
% 25/04/2022
%%%%%%%%%%%%%%%%%%
n = length(signal); % number of samples
y = fft(signal, n);% compute DFT of input signal
power = abs(y).^2/n; % power of the DFT
[val, ind] = max(power); % find the mx value of DFT and its index
% plots
figure;
subplot(1,3,1) % time plot
t=0:1/fs:(n-1)/fs; % time range
plot(t, signal)
xticks(0:0.1*fs:n*fs);
xticklabels(0:0.1:n/fs);
xlabel('Time (s)');
ylabel('Amplitude (a.u.)');
subplot(1,3,2) % linear frequency plot
f = (0:n-1)*(fs/n); % frequency range
plot(f,power, 'b*'); hold on;
plot(f,power, 'r');
xlabel('Frequency (Hz)')
ylabel('Power (a.u.)')
subplot(1,3,3) % log frequency plot
plot(f,10*log10(power/power(ind)));
xlabel('Frequency (Hz)')
ylabel('Power (dB)')

32
sampling.m Normal file
View File

@ -0,0 +1,32 @@
function signal = sampling(signal_freq_1, signal_duration, signal_phase_1, sampling_freq, signal_freq_2, signal_phase_2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function signal = sampling(signal_freq_1, signal_duration, signal_phase_1, sampling_freq, signal_freq_2, signal_phase_2)
% ex.: signal = sampling(10, 1, 0, 20, 15, 0)
%
% Inputs:
% - signal_freq_1: frequency of the 1st cosine function in Hz
% - signal_duration: duration of the signal in seconds
% - signal_phase_1: phase of the 1st signal in rad
% - sampling_freq: sampling frequency in Hz
% - signal_freq_2: frequency of the 2nd cosine function in Hz
% - signal_phase_2: phase of the 2nd signal in rad
%
%
% Output:
% - signal: an array containing the samples of a cosine function sampled at the given sampling freq (in a.u.)
% signal = cos(2*pi*signal_freq_1*t+signal_phase_1)+cos(2*pi*signal_freq_2*t+signal_phase_2)
%
% Author: Guillaume Gibert, guillaume.gibert@ecam.fr
% Date: 04/03/2024
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=-signal_duration/2:1/sampling_freq:signal_duration/2;
signal = cos(2*pi*signal_freq_1*t+signal_phase_1) +cos(2*pi*signal_freq_2*t+signal_phase_2);
figure;
plot(t, signal);
xlabel('Time (s)');
ylabel('Signal amplitude (a.u.)');
frequencySpectrum(signal, sampling_freq);

54
windowing.m Normal file
View File

@ -0,0 +1,54 @@
function signal = windowing(signal_freq, signal_duration, signal_phase, sampling_freq)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function signal = windowing(signal_freq, signal_duration, signal_phase, sampling_freq)
% ex.: signal = windowing(10, 12, 0, 50)
%
% Inputs:
% - signal_freq: frequency of the cosine function in Hz
% - signal_duration: duration of the signal in seconds
% - signal_phase: phase of the signal in rad
% - sampling_freq: sampling frequency in Hz
%
% Output:
% - signal: an array containing the samples of a cosine function sampled at the given sampling freq and windowed (in a.u.)
% signal = cos(2*pi*signal_freq*t+signal_phase)
%
% Author: Guillaume Gibert, guillaume.gibert@ecam.fr
% Date: 04/03/2024
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generates a time array
t=-signal_duration/2:1/sampling_freq:signal_duration/2;
% generates a sampled signal
signal = cos(2*pi*signal_freq*t+signal_phase);
% window duration is half of signal duration
windowDuration = signal_duration/2;
% creates rectangular time window
rectangularWin = zeros(1, length(t));
for l_sample=1:windowDuration*sampling_freq
rectangularWin(l_sample+signal_duration*sampling_freq/4) = 1;
end
figure;
plot(rectangularWin);
for l_sample=1:signal_duration*sampling_freq
signal_rect(l_sample) = signal(l_sample) * rectangularWin(l_sample);
end
figure;
plot(signal); hold on;
plot(signal_rect);
% creates the Hanning time window
% creates the Hamming time window
% creates the Balckman time window