%
% This source code is freely distributed from the "colormonogenic" website:
% http://xlim-sic.labo.univ-poitiers.fr/projets/colormonogenic/
% published in 2018,
% which presents the main research results by 
% Raphaël Soulard & Philippe Carré,
% from the XLIM Laboratory (UMR CNRS 7252),
% University of Poitiers, France.
%
% Author: R. Soulard.
%

% No dependency

% ----------------------------------------------------------------- %
% Script to illustrate the Hilbert transform & the analytic signal  %
% ----------------------------------------------------------------- %


% ----------------------------------------------------------------- %
% Hard coded input signal (band-limited oscillation):
t = [-0.1229210 -0.1185021 -0.1101423 -0.1036942 -0.0958320 -0.0837279 -0.0718887 -0.0567781 -0.0383785 -0.0239651 -0.0154211 -0.0092513 -0.0038859 ...
0.0002462 0.0057841 0.0112014 0.0136911 0.0156796 0.0172724 0.0142759 0.0076443 0.0000699 ...
-0.0090102 -0.0218059 -0.0354899 -0.0499225 -0.0688981 -0.0853658 -0.0993987 -0.1164933 -0.1296392 -0.1396271 -0.1513077 -0.1596905 -0.1668920 -0.1752461 -0.1818955 -0.1852036 -0.1863883 -0.1844367 -0.1768443 -0.1669090 -0.1552451 -0.1422573 -0.1266939 -0.1102790 -0.0915428 -0.0660475 -0.0407165 -0.0123339 ...
0.0183823 0.0450302 0.0723941 0.0989519 0.1237809 0.1516986 0.1798037 0.2065632 0.2356088 0.2615085 0.2835335 0.3026714 0.3150803 0.3234994 0.3281748 0.3312894 0.3337546 0.3306413 0.3217343 0.3056970 0.2836813 0.2598175 0.2361316 0.2087528 0.1723591 0.1316153 0.0870568 0.0398950 ...
-0.0096187 -0.0642177 -0.1226225 -0.1859446 -0.2475444 -0.3077597 -0.3681753 -0.4239260 -0.4802331 -0.5381015 -0.5945996 -0.6497285 -0.7015707 -0.7494271 -0.7928987 -0.8296296 -0.8590701 -0.8839255 -0.9052917 -0.9226828 -0.9332960 -0.9335931 -0.9259319 -0.9159064 -0.9023358 -0.8839446 -0.8600609 -0.8279645 -0.7904164 -0.7460266 -0.6992772 -0.6482429 -0.5906480 -0.5320456 -0.4667334 -0.3993310 -0.3326416 -0.2650761 -0.1980987 -0.1308989 -0.0629033 ...
0.0068623 0.0729960 0.1358413 0.1957703 0.2536717 0.3107122 0.3658042 0.4179305 0.4593354 0.4929307 0.5206379 0.5422267 0.5632051 0.5807672 0.5909296 0.5940772 0.5903884 0.5800764 0.5675680 0.5505606 0.5284691 0.5038810 0.4757054 0.4437052 0.4099135 0.3769497 0.3404692 0.3013063 0.2593550 0.2183387 0.1806294 0.1440844 0.1107057 0.0741504 0.0376942 0.0060084 ...
-0.0263717 -0.0567707 -0.0837527 -0.1074705 -0.1263471 -0.1405364 -0.1504295 -0.1616021 -0.1689171 -0.1721524 -0.1724411 -0.1661140 -0.1578163 -0.1466518 -0.1335618 -0.1154753 -0.0934650 -0.0717819 -0.0495638 -0.0306205 -0.0134502 ...
0.0045261 0.0222341 0.0416012 0.0596844 0.0756385 0.0909478 0.1025940 0.1129914 0.1243924 0.1336001 0.1415376 0.1463075 0.1477534 0.1491202 0.1450208 0.1381880 0.1299297 0.1159179 0.1037321 0.0950450 0.0859915 0.0752058 0.0600624 0.0446638 0.0278791 0.0150264 0.0083508 ...
-0.0014683 -0.0111710 -0.0194770 -0.0277389 -0.0336631 -0.0353193 -0.0362915 -0.0385810 -0.0379294 -0.0369832 -0.0356528 -0.0283862 -0.0155223 ...
0.0027363 0.0247176 0.0466890 0.0648766 0.0820727 0.1023732 0.1227070 0.1464548 0.1705793 0.1888000 0.2058737 0.2243849 0.2424501 0.2618643 0.2835678 0.2984326 0.3066299 0.3155628 0.3191742 0.3214201 0.3230820 0.3142505 0.3019488 0.28403 0.2601757 0.2355320 0.2065806 0.1727435 0.1326282 0.0869165 0.0359479 ...
-0.0152852 -0.0668173 -0.1188723 -0.1706267 -0.2288888 -0.2916142 -0.353922 -0.4177622 -0.4781458 -0.5328707 -0.5871642 -0.6404173 -0.6921052 -0.7407549 -0.7876152 -0.8289570 -0.8615776 -0.8879416 -0.9090034 -0.9274090 -0.9392620 -0.9448660 -0.9434787 -0.9304918 -0.9125239 -0.8907900 -0.8598371 -0.8246313 -0.7873697 -0.7400271 -0.6844587 -0.6231238 -0.5564811 -0.4913639 -0.4261274 -0.3549610 -0.2789743 -0.2007549 -0.1216657 -0.0414876 ...
0.0392547 0.1196346 0.2027810 0.2821879 0.3575194 0.4326976 0.5003817 0.5632255 0.6258148 0.6859207 0.7416471 0.7894165 0.8291701 0.8639510 0.8922064 0.9132311 0.9265054 0.9325326 0.9326599 0.9311884 0.9283805 0.9167457 0.8981579 0.8742729 0.8441507 0.8094665 0.7737621 0.7365932 0.6928126 0.6471311 0.5996825 0.5475779 0.4978411 0.4483471 0.3981703 0.3489793 0.3008232 0.2569986 0.2128094 0.1716049 0.1354285 0.1011874 0.0707808 0.0427511 0.0150284 ...
-0.0136766 -0.0388826 -0.0591946 -0.0749157 -0.0840580 -0.0909983 -0.0990965 -0.1074899 -0.1124239 -0.1131956 -0.1113566 -0.1083079 -0.1054554 -0.1014348 -0.0954188 -0.0892523 -0.0842096 -0.0787033 -0.0745528 -0.0694397 -0.0643965 -0.0622156 -0.0605688 -0.0619011 -0.0644051 -0.0692746 -0.0766276 -0.0863476 -0.0999878 -0.1127861 -0.1245856 -0.1394279 -0.1585252 -0.1802459 -0.2039155 -0.2272199 -0.2447492 -0.2602655 -0.2796558 -0.2988106 -0.3166374 -0.3359981 -0.3515650 -0.3616986 -0.3736019 -0.3819791 -0.3853668 -0.3891931 -0.3902952 -0.3879728 -0.3825960 -0.3738229 -0.3620425 -0.3469784 -0.3292612 ...
-0.3113509 -0.2904410 -0.2658018 -0.2416748 -0.2144084 -0.1838726 -0.1565083 -0.1302188 -0.0996608 -0.0681081 -0.0348125 ...
0.0013371 0.0332905 0.0597692 0.0824291 0.1026146 0.1246041 0.1444115 0.1616825 0.1765170 0.1851940 0.1909776 0.1907703 0.1857015 0.1775716 0.1645021 0.1467065 0.1225907 0.0918438 0.0584851 0.0250525 ...
-0.0109465 -0.0502679 -0.0929990 -0.1381644 -0.1806232 -0.2192145 -0.2569195 -0.2959341 -0.3404680 -0.3873493 -0.4329839 -0.4780115 -0.5155403 -0.5474304 -0.5793643 -0.6054490 -0.6277056 -0.6473081 -0.6601195 -0.6677372 -0.6706552 -0.6677898 -0.6574676 -0.6426049 -0.6248140 -0.5998307 -0.5700928 -0.5364386 -0.4960990 -0.4505398 -0.3988728 -0.3378842 -0.2697899 -0.2018648 -0.1341011 -0.0667636 ...
0.0024745 0.075085 0.1511886 0.2289801 0.3041367 0.3785473 0.4501618 0.5209479 0.5870160 0.6459290 0.7033971 0.7560147 0.8060037 0.8540617 0.8930887 0.9259424 0.9512381 0.9700188 0.9858961 0.9951267 1 0.9979664 0.9886142 0.9744299 0.9517073 0.9229859 0.8897328 0.8511713 0.8108946 0.7695530 0.7281699 0.6841046 0.6383701 0.5902538 0.5376274 0.4863086 0.4318970 0.3798978 0.3316512 0.2827943 0.2392419 0.1957080 0.1508337 0.1100066 0.0722220 0.0374001 0.0071582 ...
-0.0246008 -0.0575360 -0.0864486 -0.1113288 -0.1251933 -0.1292798 -0.1311267 -0.1303529 -0.1312404 -0.1314382 -0.1264474 ];
% ----------------------------------------------------------------- %
% ----------------------------------------------------------------- %
N = length(t); % number of samples

% Numerical approximation of the Hilbert transform in the FFT domain:
W = (-floor(N/2):ceil(N/2)-1)/N; % frequency coordinates
H = ifftshift(  -1i * sign(W)  ); % sampled Fourier response

% FFT-domain Hilbert transform of the input signal 't':
hilb = real(ifft(  fft(t) .* H  )); 

sa = t + 1i*hilb; % complex valued analytic signal associated to input signal
amp = abs(sa);    % instantaneous amplitude envelope
phz = angle(sa);  % instantaneous phase
% ----------------------------------------------------------------- %
% Generate illustrations of the result.
% ----------------------------------------------------------------- %

mx = 1.1*max(amp);

figure;
subplot(211);
plot(1:N,t,'k'); hold on;
plot(1:N,hilb,'g');
axis([0 N+1 -mx mx]);
title('Signal (black), Hilbert transform (green)');
subplot(212);
plot(1:N,t,'k'); hold on;
plot(1:N,amp,'b'); hold on;
plot(1:N,phz*mx/pi,'r');
axis([0 N+1 -mx mx]);
title('Signal (black), amplitude (blue), phase (red)(normalized)');

