%=======================================================================================================================
function [ thd, thd_weighted, ratio_to_all] = THDN ...
         (input_data, SamplingFreqHz, StartAnalysis, StopAnalysis, Weighting )
%         
% THDN computes the total harmonic distortion + noise ratio ratio of a SINEWAVE 
%  signal corrupted with noise from different sources (jitter, saturation, pink noise, ..)
%
% INPUT PARAMETERS:
%   input_data      : input vector to be analysed 
%   SamplingFreqHz  : sampling frequency in Hz
%   StartAnalysis   : lowest frequency for the noise estimation
%   StopAnalysis    : highest frequency for the noise estimation
%   Weighting       : 0 for A-weighting IEC 61672:2003, 1 for CCIR/ITU-468 weighting
%
% OUTPUT PARAMETERS:
%   thd             : total harmonic distortion and noise ratio in decibel
%   thd_weighted    : THD weighted by the selected perceptual law
%   ratio_to_all    : energy ratio of the useful signal to the total energy
%
%
% MAIN INTERNAL PARAMETERS IN THE CODE:
%
%   window_type     : selection of the analysis window, default is BH7
%   siglev          : rescaling of the useful signal on the plots, default is 0dB
%   glim            : remove the signal lower than this limit, default is -200dB
%   RemoveHarmonics : compute THD after removal of the even and odd harmonics of the signal
%   RemoveJitter    : avoid the noise contribution in the smearing area of a signal corrupted by jitter
%   plot_full_band  : used for PDM modulated signal. Selects the audio band instead of the full signal band
%   plot_log_scale  : plot in semilog or linear
%
%
% EXAMPLE
%
% [fid_r, message_r] = fopen ('pdm_signal_to_analyse.raw', 'r');    % open a PDM audio file at 6.144MHz
% [pdm_data] = fscanf (fid_1,'%d'); 
% [thd, thd_weighted, r] = THDN (pdm_data, 6144000, 20, 20000, 0);  % thd estimation in [20Hz..20kHz] 
% fprintf('THDN_Aw  = %8.3f  = %8.3f bits\n', thd_weighted,  abs(thd_weighted)/6.02);
%
%   THDN_Aw  = -132.327  =   21.981 bits
% >>
%
%=======================================================================================================================

...
...
...