diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience1.m b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience1.m new file mode 100644 index 0000000..d36598c --- /dev/null +++ b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience1.m @@ -0,0 +1,80 @@ +%% Experiment 1 : FFT of sinusoidal mixtures (magnitude spectra only) + +clc; +clear; +close all; + +%% ========================== PART i ========================== +%% i) Ts = 0.2 ms, t in [0, 4 s] + +Ts1 = 0.2e-3; % Sampling period (s) +Fs1 = 1 / Ts1; % Sampling frequency (Hz) +t1 = 0 : Ts1 : 4; % Time vector +N1 = length(t1); % Number of samples + +fi1 = Fs1 / N1; % Frequency resolution (Hz) +k1 = 0 : (N1-1); % FFT index vector +f_full_1 = k1 * fi1; % Full frequency axis + +% Signal: x(t) = sin(2000 t + 56) * sin(56 - 6000 t) + 2 cos(4000 t) +x1 = sin(2000*t1 + 56) .* sin(56 - 6000*t1) + 2*cos(4000*t1); + +%% i-a) Magnitude spectrum of x(t) (single-sided) + +X1 = fft(x1); % N-point FFT +half_N1 = floor(N1/2) + 1; % Length of single-sided spectrum +X1_half = X1(1:half_N1); % Positive frequencies only +f1_half = f_full_1(1:half_N1); % Frequency axis (Hz) +magX1 = abs(X1_half); % Magnitude of complex spectrum + +figure('Name','Part i-a: |X(f)| of x(t)'); +plot(f1_half, magX1, 'r-'); +title('Part i-a: magnitude spectrum |X(f)| of x(t)'); +xlabel('Frequency (Hz)'); +ylabel('|X(f)|'); +grid on; + +%% i-b) Magnitude spectrum of x^2(t) (single-sided) + +x2 = x1.^2; % x^2(t) +X2 = fft(x2); % FFT of x^2(t) + +X2_half = X2(1:half_N1); +magX2 = abs(X2_half); +% Same frequency axis f1_half + +figure('Name','Part i-b: |X_2(f)| of x^2(t)'); +plot(f1_half, magX2, 'm-'); +title('Part i-b: magnitude spectrum |X_2(f)| of x^2(t)'); +xlabel('Frequency (Hz)'); +ylabel('|X_2(f)|'); +grid on; + +%% ========================== PART ii ========================== +%% ii) Fs = 5 kHz, t in [0, 2 s] +% x(t) = cos(2000 t^2) + 0.01 sin(2000 t) + 0.01 sin(10000 t) + +Fs2 = 5000; % Sampling frequency (Hz) +Ts2 = 1 / Fs2; % Sampling period (s) +t2 = 0 : Ts2 : 2; % Time vector +N2 = length(t2); % Number of samples + +fi2 = Fs2 / N2; % Frequency resolution +k2 = 0 : (N2-1); +f_full_2 = k2 * fi2; + +x3 = cos(2000*t2.^2) + 0.01*sin(2000*t2) + 0.01*sin(10000*t2); + +X3 = fft(x3); % FFT + +half_N2 = floor(N2/2) + 1; +X3_half = X3(1:half_N2); +f2_half = f_full_2(1:half_N2); +magX3 = abs(X3_half); + +figure('Name','Part ii: |X(f)| of chirp signal'); +plot(f2_half, magX3, 'b-'); +title('Part ii: magnitude spectrum |X(f)| of x(t)'); +xlabel('Frequency (Hz)'); +ylabel('|X(f)|'); +grid on; diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience2.m b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience2.m new file mode 100644 index 0000000..2a49ce0 --- /dev/null +++ b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience2.m @@ -0,0 +1,49 @@ +%% Experiment 2 : Recreation of a signal + +clc; +clear; +close all; + +Fs = 4000; % Sampling frequency (Hz) +N = 2048; % Number of samples +t = (0:N-1)/Fs; % Time vector (s) + +df = Fs/N; % Frequency resolution (Hz) + +% --- 1) Desired peak frequencies (Hz) --- +f_target = [300 400 700 1300 1600 1700]; + +% Align each frequency with the closest FFT bin to avoid spectral leakage. +k = round(f_target/df); % Integer FFT bin indices +f = k * df; % Actual frequencies used (bin-aligned) + +% --- 2) Desired peak heights in |FFT| --- +H = [1000 2000 3250 3250 2000 1000]; + +% --- 3) Amplitudes of the cosines for those peak heights --- +% For a cosine perfectly aligned to a bin, the peak in |FFT| is about N*A/2, +% so we choose A = 2*H/N to obtain the target height H. +A = 2*H / N; + +% --- 4) Build the time-domain signal y(t) as a sum of cosines --- +% Random phases are used because the magnitude spectrum does not depend on phase. +phi = 2*pi*rand(size(f)); % Random phase for each component +y = zeros(size(t)); % Initialize signal + +for i = 1:length(f) + y = y + A(i) * cos(2*pi*f(i)*t + phi(i)); +end + +% --- 5) Compute FFT and plot the magnitude spectrum --- +Y = fft(y); % Complex FFT of y(t) +Py = abs(Y); % Magnitude of the FFT (two-sided, unnormalized) + +f_axis = (0:N-1) * df; % Frequency axis (Hz) + +figure; +stem(f_axis, Py, 'g'); % Discrete spectrum as vertical lines +xlim([0 Fs/2]); % Show only positive frequencies up to Nyquist +xlabel('Frequency (Hz)'); +ylabel('|Y(f)|'); +title('Magnitude spectrum with target peak heights'); +grid on; diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience3.m b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience3.m new file mode 100644 index 0000000..025a85a --- /dev/null +++ b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience3.m @@ -0,0 +1,31 @@ +%% Experiment 3 : Power spectrum of "holiday_offer" +% Plot the power spectrum with the same style as the TP figure. [web:81][web:155] + +clc; +clear; +close all; + +% 1) Load signal from MAT-file +data = load('holiday_offer.mat'); +varNames = fieldnames(data); +signalName = varNames{1}; +x = data.(signalName); % time-domain signal vector + +Fs = 11025; % Sampling frequency (Hz) + +% 2) FFT-based power spectrum (periodogram) +N = length(x); +Xdft = fft(x); +Pxx = (1/N) * abs(Xdft).^2; % power spectrum estimate [web:154] + +% We keep the FULL spectrum (both sides) as in the given figure +f = (0:N-1) * (Fs/N); % frequency axis from 0 to Fs (≈11025 Hz) + +% 3) Plot +figure; +plot(f, Pxx, 'g'); +xlabel('Frequency (Hz)'); +ylabel('Power'); +title('Power spectrum of holiday\_offer signal'); +grid on; +xlim([0 Fs]); diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience4.m b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience4.m new file mode 100644 index 0000000..d1aeed2 --- /dev/null +++ b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2_Experience4.m @@ -0,0 +1,58 @@ +%% Experiment 4 : Magnitude spectra of composed signals + +clc; +clear; +close all; + +%% ========================== PART i ========================== +% i) Set the sampling period to 0.5 ms and time interval [0, 2 s] +% x(t) = sin(4000 t) + 2 cos(4000 t - 32) + cos^2(2000 t) + +Ts1 = 0.5e-3; % Sampling period (s) +Fs1 = 1 / Ts1; % Sampling frequency (Hz) -> 2000 Hz +t1 = 0 : Ts1 : 2; % Time vector (s) +N1 = length(t1); % Number of samples + +% Define the signal x1(t) +x1 = sin(4000*t1) + 2*cos(4000*t1 - 32) + cos(2000*t1).^2; + +% FFT and magnitude spectrum (single-sided) +X1 = fft(x1); % Complex FFT of x1 +magX1 = abs(X1); % Magnitude spectrum +halfN1 = floor(N1/2) + 1; % First half (real signal) [web:171] +magX1_half = magX1(1:halfN1); +f1 = (0:halfN1-1) * (Fs1/N1); % Frequency axis in Hz + +figure('Name','Experiment 4 - Part i'); +plot(f1, magX1_half, 'g'); % Green magnitude spectrum +xlabel('Frequency (Hz)'); +ylabel('|X_1(f)|'); +title('Experiment 4 - Part i : Magnitude spectrum of x(t)'); +grid on; + +%% ========================== PART ii ========================== +% ii) Set the sampling frequency to 5 kHz and time interval [0, 2 s] +% x(t) = 0.1 sin(2000 * sqrt(t)) + 0.01 sin(4000 t) + 0.01 cos(7000 t) +% Note: Interpret "2000 t1/2" as 2000 * sqrt(t), i.e., t^(1/2). + +Fs2 = 5000; % Sampling frequency (Hz) +Ts2 = 1 / Fs2; % Sampling period (s) +t2 = 0 : Ts2 : 2; % Time vector (s) +N2 = length(t2); % Number of samples + +% Avoid sqrt(0) issues in instantaneous frequency by allowing t = 0 (OK in MATLAB) +x2 = 0.1 * sin(2000 * sqrt(t2)) + 0.01 * sin(4000*t2) + 0.01 * cos(7000*t2); + +% FFT and magnitude spectrum (single-sided) +X2 = fft(x2); +magX2 = abs(X2); +halfN2 = floor(N2/2) + 1; +magX2_half = magX2(1:halfN2); +f2 = (0:halfN2-1) * (Fs2/N2); % Frequency axis in Hz + +figure('Name','Experiment 4 - Part ii'); +plot(f2, magX2_half, 'g'); % Green magnitude spectrum +xlabel('Frequency (Hz)'); +ylabel('|X_2(f)|'); +title('Experiment 4 - Part ii : Magnitude spectrum of x(t)'); +grid on; diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2cipa.docx b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2cipa.docx index ebd498d..b4a5a62 100644 Binary files a/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2cipa.docx and b/ISEN/Traitement du signal/CIPA4/TP/TP2/TP2cipa.docx differ diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/holiday_offer.mat b/ISEN/Traitement du signal/CIPA4/TP/TP2/holiday_offer.mat new file mode 100644 index 0000000..3b7ba86 Binary files /dev/null and b/ISEN/Traitement du signal/CIPA4/TP/TP2/holiday_offer.mat differ diff --git a/ISEN/Traitement du signal/CIPA4/TP/TP2/~$P2cipa.docx b/ISEN/Traitement du signal/CIPA4/TP/TP2/~$P2cipa.docx new file mode 100644 index 0000000..9c77bbb Binary files /dev/null and b/ISEN/Traitement du signal/CIPA4/TP/TP2/~$P2cipa.docx differ