Signal to noise ratio – THD+N


The purpose of this MATLAB program is to compute the signal to noise ratio (SNR) of audio files.

Input files can be of various formats : pulse-density modulation (PDM), raw samples (PCM), in text or binary format.

The program concentrates its analysis in the audio and voice bands. It can weight the results using ITU/CCIR or using dBA curves.

Optionally, it can cope with jitter and other types of distortions before computing SNR.

Code folder is here


Example of analysis for a file sampled at 8kHz


Example of analysis at 6.144MHz PDM data rate using linear and log-scale.

Main function header

% Copyright (c) 2015 FIRMWARE DEVELOPMENTS SAS                                       
% All rights reserved.                                                                
% This source code and any compilation or derivative thereof is the proprietary       
% information of FIRMWARE DEVELOPMENTS SAS and is confidential in nature.             
% Under no circumstance is this software to be combined with any Open Source Software 
% in any way or placed under an Open Source License of any type without the express   
% written permission of FIRMWARE DEVELOPMENTS SAS        
function [ thd, thd_weighted, ratio_to_all] = THDN ...
         (input_data, SamplingFreqHz, StartAnalysis, StopAnalysis, Weighting, siglev, glim, print_results )
% THDN computes the total harmonic distortion + noise ratio ratio of a SINEWAVE 
%  signal corrupted with noise from different sources (jitter, saturation, pink noise, ..)
%   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
%   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
%   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
% [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
% >>