⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plotpeaks.m

📁 我认为很不错的语音处理的matlab源代码
💻 M
字号:
function [h, h2, indices2]=plotpeaks(SP, Fs, num_pts_per_pk_intrl, make_plot, Tool_Name, filename1, same_ylim, min_peak, sar, plot_str, Align_peaks, peak_alignment_tol, percent1, exhaust_cycle )
% % 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, sar, plot_str, ...
% % Align_peaks, peak_alignment_tol, percent1, exhaust_cycle );
% % 
% % ********************************************************************
% % 
% % 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 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 (Pa) is the lowest ampitude for a peak to be considered
% %          impulsive. default value is 30.
% % 
% % sar is an acronym which stands for "surpress all requirements"
% %      sar=1; allows all detected impulses to be analyzed.
% %      othwerwise only the impulses with a peak pressure above the
% %      min_peak will be analyzed.  default value is 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 is the 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.  
% % 
% % 
% % 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.   radius of the 
% %             
% % 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.  
% % 
% % percent1=[];        % Is the percentage of the peak levels of the  
% %                     % time record after signal processing to remove
% %                     % continuous noise and sharpen the peak levels.
% %                     % The default is percent1=[];    
% %                     % 
% %                     % If percent1 is empty, < 0, or > 100 then 
% %                     % it is set to [] and not considered during
% %                     % processing;   
% %                     % 
% %                     % percent1=75; is a typical value
% %                     
% % ********************************************************************
% % 
% % 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=30;              % is the peak value in Pa that is the minimum 
%                           % for being consiedered a peak.
% 
% sar=1;                    % will suspend the requiremnts for being a peak
%                           % and force the program to find at least one
%                           % peak.  Tthe program can crash if it does 
%                           % not find a peak.  This has rarely been a 
%                           % problem. 
%                           
% 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.  
% % 
% % 
% % percent1=[];        % Is the percentage of the peak levels of the  
% %                     % time record after signal processing to remove
% %                     % continuous noise and sharpen the peak levels.
% %                     % The default is percent1=[];    
% %                     % 
% %                     % If percent1 is empty, < 0, or > 100 then 
% %                     % it is set to [] and not considered during
% %                     % processing;   
% %                     % 
% %                     % percent1=75; is a typical value
% % 
% % exhaust_cycle=0;    % 1 use the threshold method approppriate for 
% %                     % finding exhaust noise type impulses.   
% %                     % 
% %                     % Otherwise the method for impact type impulsive
% %                     % noises is used.
% %                     % 
% %                     % The default value is exhaust_cycle=0;
% 
% [h, h2, indices2]=plotpeaks(SP, Fs, num_pts_per_pk_intrl, make_plot, ...
% Tool_Name, filename1, same_ylim, min_peak, sar, plot_str, ...
% Align_peaks, peak_alignment_tol, percent1, exhaust_cycle);
% 
% % ********************************************************************
% % 
% % 
% % List of Dependent Subprograms for 
% % plotpeaks
% % 
% % FEX ID# is the File ID on the Matlab Central File Exchange
% % 
% % Program Name   Author   FEX ID#
% %  1) allstats		Duane Hanselman		NA	
% %  2) calc_impuls_threshold_and_index		
% %  3) convert_double		
% %  4) fix_YTick		
% %  5) func_threshold		Jing Tian		10462	
% %  6) hilbert2		
% %  7) kurtosis2		
% %  8) localpeaks		
% %  9) parseArgs		Malcolm Wood		10670	
% % 10) peak_index		
% % 11) peak_threshhold_function		
% % 12) percentiles		
% % 13) plot_snd_vibs		
% % 14) psuedo_box		
% % 15) resample_plot		
% % 16) sub_mean		
% % 17) subaxis		Aslak Grinsted		3696	
% % 18) wsmooth		Damien Garcia			
% % 
% % ********************************************************************
% %
% % 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.  
% % 
% % ********************************************************************
% % 
% % 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)) || isnumeric(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=30;
end

if (nargin < 9 || isempty(sar)) || ~isnumeric(sar)
    sar=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(percent1)) || ~isnumeric(percent1)
    percent1=[];
end

if (nargin < 14  || isempty(exhaust_cycle)) || ~isnumeric(exhaust_cycle)
    exhaust_cycle=0;
end


% find the impulsive peaks 
[SP_local_max2, indices2]=localpeaks(SP, Fs, num_pts_per_pk_intrl, min_peak, sar, Align_peaks, 0, peak_alignment_tol, percent1, exhaust_cycle);
  
[m1, n1]=size(SP);

t_SP=1/Fs*(1:n1);

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




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -