Code covered by the BSD License  

Highlights from
Impulsive Noise Meter

image thumbnail
from Impulsive Noise Meter by Edward Zechmann
Calculates Impulsive noise metrics for hazardous acoustic noise assessent

[h, h2, indices2]=plotpeaks(SP, Fs, num_pts_per_pk_intrl, make_plot, Tool_Name, filename1, same_ylim, min_peak, ignore_threshold, plot_str, Align_peaks, peak_alignment_tol, dB_scale )
function [h, h2, indices2]=plotpeaks(SP, Fs, num_pts_per_pk_intrl, make_plot, Tool_Name, filename1, same_ylim, min_peak, ignore_threshold, plot_str, Align_peaks, peak_alignment_tol, dB_scale )
% % plotpeaks: finds peaks, plots peaks, then outputs impulsive noise metrics
% % 
% % Syntax:
% % 
% % [h, h2, indices2]=plotpeaks(SP, Fs, num_pts_per_pk_intrl, make_plot, 
% % Tool_Name, filename1, same_ylim, min_peak, ignore_threshold, plot_str, 
% % Align_peaks, peak_alignment_tol, dB_scale );  
% % 
% % ********************************************************************
% % 
% % Description
% % 
% % This progam plots the time record and puts a circle on each impulsive 
% % peak that was detected and returns the indices of each impoulsive peak.
% % 
% % ********************************************************************
% % 
% % Input Variables
% % 
% % SP is the array of sound pressure data in Pa.  
% %      default value is randn(4, 100000).
% % 
% % Fs=100000;      % Sampling rate in Hz.  
% %                 % default value is 100000 Hz.  
% % 
% % num_pts_per_pk_intrl=100000;
% %                 % Is the number of data points used to characterize
% %                 % each impulsive peak  Usually 1 second to 1/2 a
% %                 % second of data points works well. 
% %                 % default value is 1 second of datda points or the 
% %                 % length of the SP whichever is shorter.  
% %                 % default is 1 second of datapoints.
% % 
% % make_plot=1;    % plots the time records and places a circle at each 
% %                 % chosen peak. Otherwise no plots are made. 
% %                 % default value is 0.
% % 
% % Tool_Name='Hammer Drill'; % string input to determine the Name of the
% %                           % test device.  Tool_Name=1; will cause the 
% %                           % program to seach a variable named 
% %                           % Description for the Tool_Name.
% %                           % default is ''.
% % 
% % filename1='data_1.txt';   % filename to save data.  
% %                           % default is 'data_1.txt'
% % 
% % same_ylim=1;              % 1 will set all of the ylimits of each of  
% %                           % the channels to the same value.
% %                           % default is 1.
% % 
% % min_peak (dB) is the lowest ampitude for a peak to be considered
% %          impulsive. default value is 100.
% % 
% % ignore_threshold=0; % 1 surpresses the threshold and other requirements
% %                     % for selecting peaks
% %                     % ignore_threshold=1; % generally will find more peaks.
% %                     % 
% %                     % 0 is the default.  Returns typical number of
% %                     % peaks. 
% %                     % 
% %                     % default is ignore_threshold=0;
% % 
% % plot_str={'Protected', 'Unprotected'};  
% %                         % Add a string to each subaxes indicating the 
% %                         % meaning of the data.  For hearing protector 
% %                         % research one microphone is under the 
% %                         % hearing protector so it is 'Protected'
% %                         % and the other microphone is exposed so it is
% %                         % 'Unprotected'.
% %                         % default is {'Unprotected', 'Protected'}.  
% % 
% % Align_peaks=[];         % Channel number for the impulsive peaks to be
% %                         % aligned to.  deefault value is [];
% %                         % Align_peaks=1;  peaks are aligned to channel 1.  
% %                         % Align_peaks=[]; peaks are not aligned.  
% %                         % default is Align_peaks=[]; no alignment.
% % 
% % peak_alignment_tol=0.25; 
% %                     % This input sets the fraction of the datapoints 
% %                     % of num_pts_per_pk_intrl that a peak can be offset. 
% %                     % When aligning peaks, the channels are 
% %                     % synchronized, but temorally the peaks may be  
% %                     % misaligned by a few milliseconds.  The 
% %                     % alignment tolerance allows the program to find 
% %                     % the maximum peak within the alignment 
% %                     % tolerance.  
% %                     % default is 0.25.
% % 
% % One-half of the datapoints of num_pts_per_pk_intrl are used for finding the
% % index of the maximum sound pressure amplitude on either side of the 
% % center point of the Align_peaks channel.   
% %             
% % The impulsive peak is found by selecting the highest sound pressure 
% % amplitude starting at the center of the bin and working toward the 
% % maximum radius which is one-half of teh data points for the impulse.  
% % 
% % dB_scale=0;         % 1 use a dB scale to plot sound time record 
% %                     % 0 use a linear scale to plot sound time record 
% %                     %
% %                     % default is dB_scale=0; plot soudn tiem record in
% %                     % a linear scale such as Pascals.
% %  
% % ********************************************************************
% % 
% % Output Variables
% % 
% % metrics contains several pieces of information about each 
% %                    impulsive peak 
% %     
% % h is the handle for the figure. 
% % 
% % h2 is the array of handles for the subaxes.
% % 
% % ********************************************************************
% % 
% 
% Example
% 
% SP=rand(1, 50000);        % Pa sound pressure time record  waveform
% 
% Fs=50000;                 % Hz sampling rate
% 
% num_pts_per_pk_intrl=50000;    % number of data points used to characterize 
%                           % process the impulsive peak
% 
% make_plot=1;              % 1 makes plots
%                           % 0 surpresses plots
% 
% Tool_Name='Hammer Drill'; % string input to determine the Name of the
%                           % test device.  Tool_Name=1; will cause the 
%                           % program to seach a variable named Description 
%                           % for the Tool_Name.
% 
% filename1='data_1.txt';   % filename to save data
% 
% same_ylim=1;              % 1 will set all of the ylimits of each of the 
%                           % channels to the same value.
% 
% min_peak=100;             % (dB) is the peak value that is the minimum 
%                           % for being considered a peak.
% 
% ignore_threshold=0;       % 1 surpresses the threshold and other requirements
%                           % for selecting peaks
%                           % ignore_threshold=1; % generally will find more peaks.
%                           % 
%                           % 0 is the default.  Returns typical number of
%                           % peaks. 
%                           % 
%                           % default is ignore_threshold=0;
%                           
% plot_str={'Protected', 'Unprotected'};  
%                           % Add a string to each subaxes indicating the 
%                           % meaning of the data.  For hearing protector 
%                           % research one microphone is under the 
%                           % hearing protector so it is 'Protected'
%                           % and the other microphone is exposed so it is
%                           % 'Unprotected'.
%                           
% Align_peaks=1;            % Finds the peaks for the channel with the
%                           % highest LeqA then finds teh corresponding
%                           % peaks for all the other channels
%
% peak_alignment_tol=0.25; 
%                   % This input sets the fraction of the datapoints 
%                   % of num_pts_per_pk_intrl that a peak can be offset. 
%                   % When aligning peaks, the channels are 
%                   % synchronized, but temorally the peaks may be  
%                   % misaligned by a few milliseconds.  The 
%                   % alignment tolerance allows the program to find 
%                   % the maximum peak within the alignment 
%                   % tolerance. 
%                   % 
%                   % default is peak_alignment_tol=0.25; 
%  
%                   % Further explanation of peak_alignment_tol
%                   % One-half of the datapoints of num_pts_per_pk_intrl are 
%                   % used for finding the index of the maximum sound 
%                   % pressure amplitude.    
%             
%                   % The impulsive peak is found by selecting the highest 
%                   % sound pressure amplitude starting at the center of 
%                   % the bin and working toward the maximum radius which 
%                   % is one-half of teh data points for the impulse.  
% 
% 
% dB_scale=0;         % 1 use a dB scale to plot sound time record 
%                     % 0 use a linear scale to plot sound time record 
% 
% [h, h2, indices2]=plotpeaks(SP, Fs, num_pts_per_pk_intrl, make_plot, 
% Tool_Name, filename1, same_ylim, min_peak, ignore_threshold, plot_str, 
% Align_peaks, peak_alignment_tol, dB_scale );
% 
% % ********************************************************************
% % 
% % This program requires the Matlab Signal Processing Toolbox
% % This program contains code based on  oct3dsgn	by Christophe Couvreur	69
% % 
% % 
% % List of Dependent Subprograms for 
% % plotpeaks
% % 
% % FEX ID# is the File ID on the Matlab Central File Exchange
% % 
% % 
% % Program Name   Author   FEX ID#
% %  1) convert_double		Edward L. Zechmann			
% %  2) dB_to_Pa		Edward L. Zechmann			
% %  3) estimatenoise		John D'Errico		16683	
% %  4) findjobj		Yair M. Altman		14317	
% %  5) fix_YTick		Edward L. Zechmann			
% %  6) func_threshold		Jing Tian		10462	
% %  7) LMSloc		Alexandros Leontitsis		801	
% %  8) localpeaks		Edward L. Zechmann			
% %  9) maximize		Oliver Woodford		25471	
% % 10) moving		Aslak Grinsted		8251	
% % 11) Pa_to_dB		Edward L. Zechmann			
% % 12) parseArgs		Malcolm Wood		10670	
% % 13) peak_index		Edward L. Zechmann			
% % 14) peak_ix		Edward L. Zechmann			
% % 15) peak_threshhold_function2		Edward L. Zechmann			
% % 16) peakfinder		Nate Yoder		25500	
% % 17) plot_snd_vibs		Edward L. Zechmann			
% % 18) psuedo_box		Edward L. Zechmann			
% % 19) resample_plot		Edward L. Zechmann			
% % 20) sub_mean		Edward L. Zechmann			
% % 21) subaxis		Aslak Grinsted		3696	
% % 22) wsmooth		Damien Garcia		NA	
% % 
% % 
% % ********************************************************************
% %
% % plotpeaks is written by Edward L. Zechmann 
% % 
% %     date 10  August     2007
% % 
% % modified 19 December    2007    Added comments
% % 
% % modified 27 December    2007    Updated peak finding process in
% %                                 localpeaks.m.  fixed bin_size to 0.01
% %                                 seconds worth of data points      
% % 
% % modified 12 March       2008    Updated Comments and error handling
% % 
% % modified 13 August      2008    Updated Comments.  
% %                                 Added third oct linear peaks
% %  
% % modified 19 August      2008    Added input variable percent1 
% %                                 to specify threshold for 
% %                                 identifying peaks
% % 
% % modified 11 November    2008    Fixed a bug with the exhaust noise
% %                                 feature and with bin size.  
% % 
% % modified 22 March       2009    Added db_scale to select dB or linear 
% %                                 scale for plotting the sound time 
% %                                 record.
% %                                 Updated Comments.
% % 
% % modified  6 October     2009    Updated comments
% % 
% % modified 11 October     2010    Changed peak finder to peak_ix
% %                                 Updated comments.
% % 
% % modified 11 February    2011    Updated to new method of finding peaks.
% %                                 Updated Comments.
% %
% % 
% % 
% % ********************************************************************
% % 
% % Please feel free to modify this code.
% % 
% % See also: Impulsive_Noise_Meter, snd_peak_metrics, local_peaks
% % 

if (nargin < 1 || isempty(SP)) || ~isnumeric(SP)
    SP=randn(4, 100000);
end

[SP]=convert_double(SP);

[m1 n1]=size(SP);

if m1 > n1
    SP=SP';
    [m1 n1]=size(SP);
end


if (nargin < 2 || isempty(Fs)) || ~isnumeric(Fs)
    Fs=100000;
end

[Fs]=convert_double(Fs);

if (nargin < 3 || isempty(num_pts_per_pk_intrl)) || ~isnumeric(num_pts_per_pk_intrl)
    num_pts_per_pk_intrl=n1;
end

if num_pts_per_pk_intrl < 1
    num_pts_per_pk_intrl=1;
end

if num_pts_per_pk_intrl > n1;
    num_pts_per_pk_intrl=n1;
end

if (nargin < 4 || isempty(make_plot)) || ~isnumeric(make_plot)
    make_plot=1;
end


if (nargin < 5 || isempty(Tool_Name)) || isnumeric(Tool_Name)
    Tool_Name={''};
end

if (nargin < 6 || isempty(filename1)) || ~ischar(Tool_Name)
    filename1='data1.txt';
end

if (nargin < 7 || isempty(same_ylim)) || ~isnumeric(same_ylim)
    same_ylim=1;
end

if (nargin < 8 || isempty(min_peak)) || ~isnumeric(min_peak)
    min_peak=100;
end

if (nargin < 9 || isempty(ignore_threshold)) || ~isnumeric(ignore_threshold)
    ignore_threshold=0;
end

if (nargin < 10 || isempty(plot_str)) || isnumeric(plot_str)
    plot_str={'Unprotected', 'Protected'};
end

if (nargin < 11  || isempty(Align_peaks)) || ~isnumeric(Align_peaks)
    Align_peaks=[];
end

% set the peak alignment tolerance
if nargin < 12 || isempty(peak_alignment_tol) || ~isnumeric(peak_alignment_tol)
    peak_alignment_tol=0.25;
end


if nargin < 13 || isempty(dB_scale) || ~isnumeric(dB_scale)
    dB_scale=0;
end

% find the impulsive peaks 
[SP_local_max2, indices2]=localpeaks(SP, Fs, num_pts_per_pk_intrl, min_peak, ignore_threshold, Align_peaks, 0, peak_alignment_tol);


if make_plot == 1
     % plot the time records of the sound data
    [h, h2]=plot_snd_vibs(SP, Fs, [], [], Tool_Name, filename1, indices2, SP_local_max2, same_ylim, plot_str, dB_scale );
    
end




Contact us at files@mathworks.com