adding files for windowing

This commit is contained in:
Tikea TE 2025-04-16 15:40:18 +02:00
parent 185e4dec60
commit fb5b7130c5
5 changed files with 251 additions and 0 deletions

20
applyTemporalwindow.m Normal file
View File

@ -0,0 +1,20 @@
function signal_windowed = applyTemporalwindow(signal,t,win)
%{
function signal_windowed = applyTemporalWindow(signal,t,win)
Ex: i_rect = applyTemporalWindow(x,t,rect)
Task: to apply (to do the multiplication) the windowing on the signal
Inputs:
-signal: the signal we want to aplly windowing to
-t: time vector (in s)
-win: type of window
Output:
amplitude of signal_windowed
Author: tikea TE
Date: 13/04/2025
%}
for I_sample = 1:length(t)
signal_windowed(I_sample) = signal(I_sample) * win(I_sample);
end

47
createtemporalWindow.m Normal file
View File

@ -0,0 +1,47 @@
function win = createTemporalWindow(t, duration_signal, duration_win, sampling_freq, win_type)
%{
fuction win = createTemporalWindow(t, duration_signal, duration_win, sampling_freq, win_type)
Ex: win = createTemporalWindow(t, 6, 2, 40, 1)
Task: create the 4 types of windowing functions
Inputs:
-t: time vector of the signal
-duration_signal: duration of the signal (in s)
-duration_win: duration of the window
-sampling_freq: how many samples per second
-win_type: 1 for rectangle, 2 for hamming, 3 for hanning, 4 for blackman
Output:
win : window amplitude with the same duraion as the temporal vector
Author: Tikea TE
Date: 13/04/2025
%}
% create a vector of 0 the same length as the signal
win = zeros(1,length(t));
for I_sample = 1:duration_win * sampling_freq % nuber of samples in the window
% for rectangle
if (win_type == 1)
win(I_sample + (duration_signal - duration_win)/2 * sampling_freq ) = 1;
end
% for hanning
if (win_type == 2)
win(I_sample + (duration_signal - duration_win)/2 * sampling_freq ) = 0.5 - 0.5*cos(2*pi*I_sample/(duration_win*sampling_freq));
end
% for hamming
if (win_type == 3)
win(I_sample + (duration_signal - duration_win)/2 * sampling_freq ) = 0.54 - 0.46*cos(2*pi*I_sample/(duration_win*sampling_freq));
end
% for blackman
if (win_type == 4)
win(I_sample + (duration_signal - duration_win)/2 * sampling_freq ) = 0.42 - 0.5*cos(2*pi*I_sample/(duration_win*sampling_freq)) + 0.08*cos(4*pi*I_sample/(duration_win*sampling_freq));
end
end

25
main.m
View File

@ -28,8 +28,33 @@ plotRawSignal(X, Fs);
% ======== Apply FirFilter ===============
filteredSignal = firFilter(30, [5 20], X, 200); % for a 30th-order bandpass FIR filter
% ============ plot the filtered signal ============
plotFilteredSignal(filteredSignal, Fs);
plotRawVsFiltered(X, filteredSignal, Fs);
% ==== Power spectrum of the filtered signal ====
[f_filtered, power_filtered] = frequencySpectrum(filteredSignal, Fs, 1);
% === Create time vector for windowing ===
t = (0:length(filteredSignal)-1) / Fs;
% === Choose window duration ===
duration_signal = length(filteredSignal) / Fs;
win_duration = 2; % seconds
% === Create and apply Hanning window ===
win_hanning = createTemporalWindow(t, duration_signal, win_duration, Fs, 2);
x_hanning = applyTemporalwindow(filteredSignal, t, win_hanning);
% === Plot raw vs windowed ===
figure;
plot(t, filteredSignal, 'r'); hold on;
plot(t, x_hanning, 'b');
legend('Filtered signal', 'Hanning-windowed');
xlabel('Time (s)');
ylabel('Amplitude');
title('Windowing Effect on Filtered Signal');
grid on;
end

48
sampling.m Normal file
View File

@ -0,0 +1,48 @@
function [t,x] = sampling(freq_signal, sampling_freq, phase_signal, duration, plt)
%{
function [t,x] = sampling(freq_signal, sampling_freq, phase_signal, duration, plt)
ex: [t,x] = sampling(10,30,1,1)
Task: generate a cosine function knowing frequency, sampling freq, phase and duration
Inputs:
-freq_signal: frequency of the signal (in hz)
-sampling_freq: sampling frequency (how many sample per second) (in hz)
-phase_signal: signal phase (in rad)
-duration: how long u want the signal to be
plt: if greater than 0, signal will be plotted
Outputs:
-t: time vector
-x: amplitude of the cosine signal
-a figure of a cosine signal
Author: Tikea TE
Date: 11/04/2025
%}
% generate the time vector
t = -duration/2: 1/sampling_freq: duration/2;
% generate the cosine signal
x = cos(2*pi*freq_signal*t + phase_signal);
% plotting the figure
if(plt)
figure;
plot(t,x,'b');
xlabel('Time(s)');
ylabel('signal amplitude(a.u.)');
title(['cosine signal at ', num2str(freq_signal), 'Hz']); % use [] for joining string
grid on;
end

111
windowing.m Normal file
View File

@ -0,0 +1,111 @@
function windowing(freq_signal, sampling_freq, phase_signal,win_duration, duration_signal, plt)
%{
fuction windowing(freq_signal, sampling_freq, phase_signal,win_duration, duration_signal, plt)
Ex: windowing(10, 40, 0, 2, 6, 1)
Task: to apply the time window of the signal x (any signal)
Inputs:
-win_sig: duration of the window
-freq_signal: frequency of the signal (in hz)
-sampling_freq: sampling frequency (how many sample per second) (in hz)
-phase_signal: signal phase (in rad)
-duration: how long u want the signal to be
-plt: if greater than 0, signal will be plotted
Output:
none
%}
[t,x] = sampling(freq_signal, sampling_freq, phase_signal, duration_signal, 0);
% create the rectangle window:
rect = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 1);
% apply the rectangle window
I_rect = applyTemporalwindow(x,t,rect);
% plot the rectangular window
if (plt)
figure;
plot(t, x, 'r'); hold on;
plot(t, I_rect, 'b');
xlabel('Time (s)');
ylabel('Signal amplitude (n.u.)');
legend('original', 'windowed');
end
% create the Hanning window:
Hanning = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 2);
% apply the hanning window
I_hanning = applyTemporalwindow(x,t,Hanning);
% plot the hanning window
if (plt)
plot(t,I_hanning,'g');
legend('original', 'rect','hanning');
end
% create the Hamming window:
Hamming = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 3);
% apply the hamming window
I_hamming = applyTemporalwindow(x,t,Hamming);
% plot the hamming window
if (plt)
plot(t,I_hamming,'k');
legend('original', 'rect','hanning','hamming');
end
% create the blackman window:
blackman = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 4);
% apply the blackman window
I_blackman = applyTemporalwindow(x,t,blackman);
% plot the rectangular window
if (plt)
plot(t,I_blackman,'c');
legend('original', 'rect','hanning','hamming','blackman');
end
% compute power for each window
[f_rect, P_rect] = frequencySpectrum(I_rect,sampling_freq,0);
[f_hanning, P_hanning] = frequencySpectrum(I_hanning,sampling_freq,0);
[f_hamming, P_hamming] = frequencySpectrum(I_hamming,sampling_freq,0);
[f_blackman, P_blackman] = frequencySpectrum(I_blackman,sampling_freq,0);
% plotting
if(plt)
figure;
% find max value and its index of DFT
[val, ind] = max(P_rect);
plot(f_rect,10*log10(P_rect/P_rect(ind)),'b');
hold on;
% find max value and its index of DFT
[val, ind] = max(P_hanning);
plot(f_hanning,10*log10(P_hanning/P_hanning(ind)),'r');
hold on;
% find max value and its index of DFT
[val, ind] = max(P_hamming);
plot(f_hamming,10*log10(P_hamming/P_hamming(ind)),'k');
hold on;
% find max value and its index of DFT
[val, ind] = max(P_blackman);
plot(f_blackman,10*log10(P_blackman/P_blackman(ind)),'g');
hold on;
xlabel('frequency (hz)');
ylabel('Power (dB)');
legend('rect','hanning','hamming','blackman');
xlim([0 sampling_freq]);
end