From 7ff7e221dd0dd9bf9b16d302542dfa2266fcf6b0 Mon Sep 17 00:00:00 2001
From: Antonio Emilio <antonio_pereira@outlook.com>
Date: Fri, 21 Mar 2025 12:08:47 +0100
Subject: [PATCH] feat

---
 src-ref/ADCSamplesOctave.csv  |  48 +++++-
 src-ref/coeficientes_vhdl.txt | 294 +++++++++++++++++-----------------
 src-ref/octaveScript.m        |  68 ++++----
 src-ref/output.txt            | 142 +++++++++++++++-
 src/hdl/operativeUnit.vhd     |   5 -
 5 files changed, 367 insertions(+), 190 deletions(-)

diff --git a/src-ref/ADCSamplesOctave.csv b/src-ref/ADCSamplesOctave.csv
index 6f564ab..17cc7f2 100644
--- a/src-ref/ADCSamplesOctave.csv
+++ b/src-ref/ADCSamplesOctave.csv
@@ -1,3 +1,4 @@
+
 0,0,-23
 1,1023,-23
 2,0,-24
@@ -48,4 +49,49 @@
 47,-6,-12
 48,-6,-12
 49,-3,-12
-50,-1,-12
\ No newline at end of file
+50,-1,-12
+51,0,-13
+52,2,-13
+53,4,-12
+54,0,-12
+55,-1,-11
+56,1,-10
+57,1,-10
+58,2,-9
+59,5,-8
+60,8,-7
+61,11,-6
+62,12,-5
+63,9,-4
+64,8,-2
+65,9,-1
+66,8,1
+67,8,2
+68,14,4
+69,15,5
+70,18,5
+71,21,6
+72,21,6
+73,23,6
+74,21,7
+75,20,7
+76,20,8
+77,19,9
+78,25,11
+79,31,13
+80,32,15
+81,32,17
+82,35,19
+83,35,20
+84,31,21
+85,32,22
+86,30,23
+87,30,24
+88,32,25
+89,36,25
+90,38,25
+91,38,25
+92,40,25
+93,38,24
+94,36,24
+95,35,23
\ No newline at end of file
diff --git a/src-ref/coeficientes_vhdl.txt b/src-ref/coeficientes_vhdl.txt
index c4d76a2..c5fa44b 100644
--- a/src-ref/coeficientes_vhdl.txt
+++ b/src-ref/coeficientes_vhdl.txt
@@ -1,154 +1,154 @@
-
 --- Coefficients du filtre Baseline ---
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-2, 11),
-to_signed(-2, 11),
-to_signed(-2, 11),
-to_signed(-3, 11),
-to_signed(-3, 11),
-to_signed(-3, 11),
-to_signed(-4, 11),
-to_signed(-4, 11),
-to_signed(-5, 11),
-to_signed(-5, 11),
-to_signed(-6, 11),
-to_signed(-6, 11),
-to_signed(-7, 11),
-to_signed(-7, 11),
-to_signed(-8, 11),
-to_signed(-8, 11),
-to_signed(-9, 11),
-to_signed(-10, 11),
-to_signed(-10, 11),
-to_signed(-11, 11),
-to_signed(-11, 11),
-to_signed(-12, 11),
-to_signed(-13, 11),
-to_signed(-13, 11),
-to_signed(-14, 11),
-to_signed(-14, 11),
-to_signed(-15, 11),
-to_signed(-15, 11),
-to_signed(-16, 11),
-to_signed(-16, 11),
-to_signed(-17, 11),
-to_signed(-17, 11),
-to_signed(-18, 11),
-to_signed(-18, 11),
-to_signed(-18, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(1004, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-19, 11),
-to_signed(-18, 11),
-to_signed(-18, 11),
-to_signed(-18, 11),
-to_signed(-17, 11),
-to_signed(-17, 11),
-to_signed(-16, 11),
-to_signed(-16, 11),
-to_signed(-15, 11),
-to_signed(-15, 11),
-to_signed(-14, 11),
-to_signed(-14, 11),
-to_signed(-13, 11),
-to_signed(-13, 11),
-to_signed(-12, 11),
-to_signed(-11, 11),
-to_signed(-11, 11),
-to_signed(-10, 11),
-to_signed(-10, 11),
-to_signed(-9, 11),
-to_signed(-8, 11),
-to_signed(-8, 11),
-to_signed(-7, 11),
-to_signed(-7, 11),
-to_signed(-6, 11),
-to_signed(-6, 11),
-to_signed(-5, 11),
-to_signed(-5, 11),
-to_signed(-4, 11),
-to_signed(-4, 11),
-to_signed(-3, 11),
-to_signed(-3, 11),
-to_signed(-3, 11),
-to_signed(-2, 11),
-to_signed(-2, 11),
-to_signed(-2, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(-1, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
-to_signed(0, 11),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-2, 12),
+to_signed(-2, 12),
+to_signed(-2, 12),
+to_signed(-3, 12),
+to_signed(-3, 12),
+to_signed(-3, 12),
+to_signed(-4, 12),
+to_signed(-4, 12),
+to_signed(-5, 12),
+to_signed(-5, 12),
+to_signed(-6, 12),
+to_signed(-6, 12),
+to_signed(-7, 12),
+to_signed(-7, 12),
+to_signed(-8, 12),
+to_signed(-8, 12),
+to_signed(-9, 12),
+to_signed(-10, 12),
+to_signed(-10, 12),
+to_signed(-11, 12),
+to_signed(-11, 12),
+to_signed(-12, 12),
+to_signed(-13, 12),
+to_signed(-13, 12),
+to_signed(-14, 12),
+to_signed(-14, 12),
+to_signed(-15, 12),
+to_signed(-15, 12),
+to_signed(-16, 12),
+to_signed(-16, 12),
+to_signed(-17, 12),
+to_signed(-17, 12),
+to_signed(-18, 12),
+to_signed(-18, 12),
+to_signed(-18, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(1004, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-19, 12),
+to_signed(-18, 12),
+to_signed(-18, 12),
+to_signed(-18, 12),
+to_signed(-17, 12),
+to_signed(-17, 12),
+to_signed(-16, 12),
+to_signed(-16, 12),
+to_signed(-15, 12),
+to_signed(-15, 12),
+to_signed(-14, 12),
+to_signed(-14, 12),
+to_signed(-13, 12),
+to_signed(-13, 12),
+to_signed(-12, 12),
+to_signed(-11, 12),
+to_signed(-11, 12),
+to_signed(-10, 12),
+to_signed(-10, 12),
+to_signed(-9, 12),
+to_signed(-8, 12),
+to_signed(-8, 12),
+to_signed(-7, 12),
+to_signed(-7, 12),
+to_signed(-6, 12),
+to_signed(-6, 12),
+to_signed(-5, 12),
+to_signed(-5, 12),
+to_signed(-4, 12),
+to_signed(-4, 12),
+to_signed(-3, 12),
+to_signed(-3, 12),
+to_signed(-3, 12),
+to_signed(-2, 12),
+to_signed(-2, 12),
+to_signed(-2, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(-1, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
+to_signed(0, 12),
 
 --- Coefficients du filtre Pei-Tseng A ---
-to_signed(1024, 11),
-to_signed(-1554, 11),
-to_signed(897, 11),
+to_signed(1024, 12),
+to_signed(-1554, 12),
+to_signed(897, 12),
 
 --- Coefficients du filtre Pei-Tseng B ---
-to_signed(961, 11),
-to_signed(-1554, 11),
-to_signed(961, 11),
+to_signed(961, 12),
+to_signed(-1554, 12),
+to_signed(961, 12),
 
 --- Coefficients du filtre Parks-McClellan ---
-to_signed(-119, 11),
-to_signed(122, 11),
-to_signed(149, 11),
-to_signed(191, 11),
-to_signed(226, 11),
-to_signed(239, 11),
-to_signed(226, 11),
-to_signed(191, 11),
-to_signed(149, 11),
-to_signed(122, 11),
-to_signed(-119, 11),
+to_signed(-119, 12),
+to_signed(122, 12),
+to_signed(149, 12),
+to_signed(191, 12),
+to_signed(226, 12),
+to_signed(239, 12),
+to_signed(226, 12),
+to_signed(191, 12),
+to_signed(149, 12),
+to_signed(122, 12),
+to_signed(-119, 12),
+
diff --git a/src-ref/octaveScript.m b/src-ref/octaveScript.m
index 05c72f5..0093612 100644
--- a/src-ref/octaveScript.m
+++ b/src-ref/octaveScript.m
@@ -2,16 +2,17 @@
 % https://archive.physionet.org/cgi-bin/atm/ATM
 % Echantillonne à 500Hz (F_Nyquist = 250Hz)
 % Script OCTAVE (pas matlab...)
+fid = fopen("values.txt", "w");
 
 Fs = 500;     % Frequence d'echantillonnage
 Fn = Fs/2;    % Frequence de Nyquist
-figure(1)
+%figure(1)
 T = csvread('./ADCSamplesOctave.csv');
-% subplot(2,3,1);plot(T(:,2));title('Raw ECG signal');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (output of an 11-bit ADC)');
+%subplot(2,3,1);plot(T(:,2));title('Raw ECG signal');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (output of an 11-bit ADC)');
 
 % Pour Octave (à supprimer sous Matlab)
 pkg load signal;
-fid = fopen("output.txt", "w");
+
 % Pour les trois filtres suivants, on peut jouer sur les ordres
 % donc le nombre de coefficients des filtres numériques
 
@@ -19,37 +20,33 @@ fid = fopen("output.txt", "w");
 fBaseLine = fir1(128, 5/Fn, 'high'); % Génération du filtre
 fBaseLine = round(fBaseLine * 2^10); % Normalisation et mise à l'échelle
 y_minus_BL = filter(fBaseLine, [1], T(:,2)); % Application du filtre
-
+fprintf(fid, "\n--- 1er Filtre ---\n");
 for i = 1:length(y_minus_BL)
     fprintf(fid, "%f\n", y_minus_BL(i));
 end
 
 % subplot(2,3,2);plot(y_minus_BL);title('Baseline wander reduced');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
 % subplot(2,3,3);plot(y_minus_BL(1:1000));title('Baseline wander reduced -- zoomed');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
-fprintf(fid, "----------------\n");
 
 % Élimination du bruit à 50Hz par un coupe-bande basique (Notch FIR)
 f50Hz = fir1(100, [45 55]/Fn, 'stop'); % Génération du filtre
 f50Hz = round(f50Hz * 2^10); % Normalisation et mise à l'échelle
 y_minus_50Hz_simple = filter(f50Hz, [1], y_minus_BL); % Application du filtre
 
-for i = 1:length(y_minus_50Hz_simple)
-    fprintf(fid, "%f\n", y_minus_50Hz_simple(i));
-end
-
 % subplot(2,3,4);plot(y_minus_50Hz_simple(1:1000));title('FIR1 band-cut-- zoomed');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
-fprintf(fid, "----------------\n");
+
 % Élimination du bruit à 50Hz par un coupe-bande plus élaboré (Pei-Tseng) IIR
 [b, a] = pei_tseng_notch(50 / Fn, 10/Fn);
 b = round(b * 2^10);
 a = round(a * 2^10);
 y_minus_50Hz_pei_tseng = filter(b, a, y_minus_BL);
+fprintf(fid, "\n--- 2eme Filtre ---\n");
 for i = 1:length(y_minus_50Hz_pei_tseng)
     fprintf(fid, "%f\n", y_minus_50Hz_pei_tseng(i));
 end
 
 % subplot(2,3,5);plot(y_minus_50Hz_pei_tseng(1:1000));title('Pei Tseng band-cut -- zoomed');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
-fprintf(fid, "----------------\n");
+
 % Lissage du bruit haute fréquence par filtre de Parks-McClellan (Low-pass FIR)
 Fpass  = 50;
 Fstop = 60;
@@ -58,12 +55,17 @@ A     = [1 1 0 0];
 fLP = remez(10, F, A); % Génération du filtre
 fLP = round(fLP * 2^10); % Normalisation et mise à l'échelle
 yLP = filter(fLP, [1], y_minus_50Hz_pei_tseng); % Application du filtre
+fprintf(fid, "\n--- 3eme Filtre ---\n");
+for i = 1:length(yLP)
+    fprintf(fid, "%f\n", yLP(i));
+end
 
+fclose(fid);
 
 % subplot(2,3,6);plot(yLP(1:1000));title('Low-pass filter to suppress high-freq noise -- zoomed');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
-figure(2)
+% figure(2)
 % subplot(2,1,1);plot(T(:,2));title('Raw ECG signal');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
-subplot(2,1,2);plot(yLP);title('After 3 filters');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
+% subplot(2,1,2);plot(yLP);title('After 3 filters');xlabel('Samples (Fs=500Hz)');ylabel('Magnitude (digital signal)');
 % print(2, "ECG_raw_3filters.pdf", "-dpdflatexstandalone");
 
 % figure(3)
@@ -74,37 +76,33 @@ subplot(2,1,2);plot(yLP);title('After 3 filters');xlabel('Samples (Fs=500Hz)');y
 
 % EXPORT DES COEFFICIENTS POUR VHDL
 % Conversion en entiers signés et exportation
-%printf("\n--- Coefficients du filtre Baseline ---\n");
-
+printf("\n--- Coefficients du filtre Baseline ---\n");
+fid = fopen("coeficientes_vhdl.txt", "w");
 
-for i = 1:length(yLP)
-    fprintf(fid, "%f\n", yLP(i));
+fprintf(fid, "\n--- Coefficients du filtre Baseline ---\n");
+for i = 1:length(fBaseLine)
+    fprintf(fid, "to_signed(%d, 12),\n", fBaseLine(i));
 end
-%fprintf(fid, "\n--- Coefficients du filtre Baseline ---\n");
-%for i = 1:length(fBaseLine)
-%    fprintf(fid, "to_signed(%d, 11),\n", fBaseLine(i));
-%end
 
 %fprintf(fid, "\n--- Coefficients du filtre Notch 50Hz ---\n");
 %for i = 1:length(f50Hz)
 %    fprintf(fid, "to_signed(%d, 11),\n", f50Hz(i));
 %end
 
-%fprintf(fid, "\n--- Coefficients du filtre Pei-Tseng A ---\n");
-%for i = 1:length(a)
-%    fprintf(fid, "to_signed(%d, 11),\n", a(i));
-%end
+fprintf(fid, "\n--- Coefficients du filtre Pei-Tseng A ---\n");
+for i = 1:length(a)
+    fprintf(fid, "to_signed(%d, 12),\n", a(i));
+end
 
-%fprintf(fid, "\n--- Coefficients du filtre Pei-Tseng B ---\n");
-%for i = 1:length(b)
-%    fprintf(fid, "to_signed(%d, 11),\n", b(i));
-%end
+fprintf(fid, "\n--- Coefficients du filtre Pei-Tseng B ---\n");
+for i = 1:length(b)
+    fprintf(fid, "to_signed(%d, 12),\n", b(i));
+end
 
-%fprintf(fid, "\n--- Coefficients du filtre Parks-McClellan ---\n");
-%for i = 1:length(fLP)
-%    fprintf(fid, "to_signed(%d, 11),\n", fLP(i));
-%end
+fprintf(fid, "\n--- Coefficients du filtre Parks-McClellan ---\n");
+for i = 1:length(fLP)
+    fprintf(fid, "to_signed(%d, 12),\n", fLP(i));
+end
 
 fclose(fid);
-printf("\nThe coefficients were saved in 'ouutput.txt'.\n");
-
+printf("\nThe coefficients were saved in 'coeficientes_vhdl.txt'.\n");
\ No newline at end of file
diff --git a/src-ref/output.txt b/src-ref/output.txt
index 676bef7..21ce3c4 100644
--- a/src-ref/output.txt
+++ b/src-ref/output.txt
@@ -1,3 +1,4 @@
+--- 1er Filtre ---
 0.000000
 0.000000
 0.000000
@@ -49,7 +50,53 @@
 -24486.000000
 -25107.000000
 -26763.000000
-----------------
+-27235.000000
+-28672.000000
+-29249.000000
+-30797.000000
+-31295.000000
+-32649.000000
+-33143.000000
+-33465.000000
+-34981.000000
+-35255.000000
+-35672.000000
+-35878.000000
+-36196.000000
+-36506.000000
+1009986.000000
+-36510.000000
+227.000000
+65667.000000
+129260.000000
+187725.000000
+-24898.000000
+95964.000000
+226027.000000
+155609.000000
+-42624.000000
+-44390.000000
+-44026.000000
+-45860.000000
+-46468.000000
+-46823.000000
+-44298.000000
+-41741.000000
+-34058.000000
+-35431.000000
+-32899.000000
+-30215.000000
+-29766.000000
+-31178.000000
+-31720.000000
+-28999.000000
+-27477.000000
+-27051.000000
+-23356.000000
+-21751.000000
+-21176.000000
+
+--- 2eme Filtre ---
 0.000000
 0.000000
 0.000000
@@ -101,7 +148,53 @@
 -24137.255157
 -24777.309502
 -26451.984462
-----------------
+-26945.407453
+-28413.650507
+-29013.299221
+-30562.876450
+-31048.854104
+-32396.674904
+-32892.561201
+-33287.651091
+-34850.523827
+-35134.899412
+-35595.680482
+-35863.484079
+-36243.422721
+-36587.083681
+945504.429824
+-134330.197257
+-28628.288173
+101243.111443
+200587.752612
+257361.297455
+27909.642727
+120932.736309
+202198.667674
+93999.765719
+-98498.703102
+-62759.394014
+-22913.429312
+2318.223906
+8259.347698
+-5958.507345
+-30401.698534
+-56807.002377
+-69767.929793
+-76855.118913
+-64586.840592
+-42372.723257
+-20683.370049
+-6714.778608
+-2460.172874
+-6188.641831
+-18783.442712
+-33987.353762
+-41778.182259
+-43984.508697
+-38936.616240
+
+--- 3eme Filtre ---
 0.000000
 0.000000
 0.000000
@@ -153,3 +246,48 @@
 -24912240.319270
 -26671912.997716
 -28123611.818423
+-29722983.682499
+-31353008.136208
+-32902754.809049
+-34472904.986410
+-35993833.420082
+-37315223.769529
+-38956645.652377
+-40310388.190944
+-41601726.977095
+-42877312.450321
+-44215259.181000
+-45315046.097985
+-46432718.247961
+-47263601.975037
+-165063443.613073
+82635864.183488
+84053126.781316
+107675150.530836
+143014781.783280
+178383412.365290
+239883069.049862
+226339160.255684
+222139075.017194
+260039255.056026
+75733340.776994
+206132419.639837
+174057111.478851
+121765868.649610
+64585776.846645
+10622692.372057
+21639753.788805
+-9925315.537044
+-46310786.614378
+-49416044.621109
+-28270924.616315
+-42469458.655877
+-60124038.423241
+-73218879.682026
+-77523797.032443
+-71686328.428358
+-57954422.972234
+-41215339.851185
+-28781460.064110
+-21016584.151478
+-21577312.032120
\ No newline at end of file
diff --git a/src/hdl/operativeUnit.vhd b/src/hdl/operativeUnit.vhd
index abb24b1..2f437d2 100644
--- a/src/hdl/operativeUnit.vhd
+++ b/src/hdl/operativeUnit.vhd
@@ -370,11 +370,6 @@ BEGIN
     END IF;
   END PROCESS store_result;
 
-
-
-
-
-
   O_total <= STD_LOGIC_VECTOR(SR_Y);
 
 END ARCHITECTURE arch_operativeUnit;
\ No newline at end of file
-- 
GitLab