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

📄 plot_snd_vibs.m

📁 我认为很不错的语音处理的matlab源代码
💻 M
字号:
function [h, h2, wb_th_array]=plot_snd_vibs(SP, t_SP, vibs, t_vibs, Tool_Name, filename1, indices2, SP_local_max2, same_ylim, plot_str  )
% % plot_snd_vibs(: plots sound and vibrations data in the time domain
% % 
% % Syntax: 
% % 
% % [h, h2, wb_th_array]=plot_snd_vibs(SP, t_SP, vibs, t_vibs, Tool_Name, filename1, indices2, SP_local_max2, same_ylim, plot_str  );
% % 
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Description
% %  
% % This progam plots the time record and appends a circle at points 
% % specified by indices2.  for sound and vibrations data and 
% % returns a handle to the figure and an array of handles to the subaxes.
% % 
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% % Input Arguments
% %
% % 
% %
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Output Arguments
% %
% %
% %
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Example='';
% SP=rand(1, 50000);        % Pa sound pressure time record  waveform
% Fs_SP=50000;              % Hz sampling rate
% vibs=rand(1, 50000);      % m/s^2 acceleration time record  waveform
% Fs_vibs=50000;            % Hz sampling rate
% 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 that appears in the plot title
% indices2={};              % indices to append circles to indicate the
%                           % locations of the impulsive peaks. 
%                           % 
% SP_local_max2={};         % sound pressures of the impulsive peaks 
% same_ylim=1;              % 1 will set all of the ylimits of each of the 
%                           % channels to the same value.
% 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'.
%                           
% [h, h2, wb_th_array]=plot_snd_vibs(SP, t_SP, vibs, t_vibs, Tool_Name, filename1, indices2, SP_local_max2, same_ylim, plot_str  );
%
% % Output Variables
% %     
% % h is the handle for the figure. 
% % 
% % h2 is the array of handles for the subaxes.
% % 
% % wb_th_array is the array of handles for the text descriptions of the
% % sensors.
% % 
% % 
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% % Program Written by Edward L. Zechmann 
% %
% %     date 10 August      2007
% %
% % modified 19 December    2007    added comments
% %
% % modified  7 January     2008    fixed a big in initializing 
% %                                 plot_str  
% %                                 made input arguments optional 
% %                                 added additional comments
% %
% % modified  5 September   2008    Updated Comments
% %     
% %  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Please feel free to modify this code.
% % 

if nargin < 1
    SP=randn(2, 50000);
end

if nargin < 2
    t_SP=1/50000*(1:50000);
end

if nargin < 3
    vibs=randn(2, 50000);
end

if nargin < 4
    t_vibs=1/50000*(1:50000);
end

if nargin < 5
    Tool_Name='';
end

if nargin < 6
    filename1='';
end

if nargin < 7
    indices2={};
end

if nargin < 8
    SP_local_max2={};
end

if nargin < 9
    same_ylim=0;
end

% make sure that input data is double precision.  
[SP, vibs, t_SP, t_vibs]=convert_double(SP, vibs, t_SP, t_vibs);

% The amount of space between the margin and the waveform
per_mar=0.16; 

% initilize the figure margins, spacings, and dimensions
sh=0.0;
sv=0.0;
ml=0.14;
mr=0.1;
mt=0.08;
mb=0.12;

% determine the number of sub plots
[m1, n1]=size(SP);
[m2, n2]=size(vibs);
last_x_axis=m1+m2;
        
if m1 > 0 && m2 > 0
    nn=m1+m2+1;
else
    nn=m1+m2;
end

% initialize the figures
h=figure(1); 
delete(h);
h=figure(1);
h1=[];
h2=[];
wb_th_array=zeros(1, m1+m2);

% determine the y-axis limits
% the uppeer and lower limits are the same
if same_ylim == 1
    ylim1=per_mar*ceil( 10*(max(max(abs(SP)))) );
end

% determine the x-axis limits
min_t=min([min(t_SP'), min(t_vibs')]);
max_t=max([max(t_SP'), max(t_vibs')]);

% determine whether to resample the plot or not
% data processing becomes too slow when the number of data points 
% becomes too high.
% 1,000,000 data points is the cutoff for implementing thr resampling
% routine
tot_num_samples=m1*n1+m2*n2;
flag_rs=0;
if tot_num_samples > 1000000
	flag_rs=1;
end

% initialize the plot_str
if nargin < 10 
    plot_str=cell(last_x_axis, 1);
    for e1=1:last_x_axis;
        plot_str{e1,1}='';
    end
end

% if the plot_str is too small then add more emmpty string elements
if length(plot_str) < last_x_axis
    for e1=(length(plot_str)+1):last_x_axis;
        plot_str{e1}='';
    end
end

% plot the sound data
for e2=1:m1;

    if e2 == 1
        subaxis(nn, 1, 1, 'sh', sh, 'sv', sv , 'pl', 0, 'pr', 0, 'pt', 0, 'pb', 0, 'ml', ml, 'mr', mr, 'mt', mt, 'mb', mb); 
    else
        subaxis(nn, 1, e2, 'sh', sh, 'sv', sv , 'pl', 0, 'pr', 0, 'pt', 0, 'pb', 0, 'ml', ml, 'mr', mr, 'mt', mt, 'mb', mb); 
    end
    t_SP_rs=[];
    SP_rs=[];
    if flag_rs == 1
        [t_SP_rs, SP_rs]=resample_plot(t_SP, SP(e2, :));
        plot(t_SP_rs, SP_rs');
        clear('t_SP_rs');
        clear('SP_rs');
    else
        plot(t_SP, SP(e2, :)');
    end
	hold on;
	h1=gca;
	h2=[h2 h1];
    
    if e2 == floor((m1+1)/2)
        ylabel('Sound (Pa)', 'Fontsize', 16);
    end
    
    if same_ylim ~= 1
        ylim1=per_mar*ceil( 10*(max(max(abs(SP(e2, :))))) );
    end
    
    ylim(ylim1*[-1 1]);
    xlim([min_t max_t]);
    set(gca, 'Fontsize', 12, 'box', 'off' );
    hold on;
    if e2 == 1
        title( [Tool_Name, ', ', filename1], 'Interpreter', 'none', 'Fontsize', 15 );
    end

    if e2 ~= last_x_axis
        set(gca, 'xtick', [], 'XTickLabel', '');
    end
    
    [ytick_m, YTickLabel1, ytick_good, ytick_new, yticklabel_new]=fix_YTick(0);
    wb_th=text( max_t-0.02*(max_t-min_t), -0.98*ylim1, ['Mic ', num2str(e2), ' ', plot_str{e2}], 'Fontsize', 10, 'Color', [0 0 0], 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom', 'BackgroundColor', 'none', 'Interpreter', 'none' );
    
    wb_th_array(e2)=wb_th;
    
    if nargin >= 8 
        if max(size(indices2)) >= e2 && max(size(SP_local_max2)) >= e2 
            
            plot((t_SP(2)-t_SP(1))*indices2{e2}, SP_local_max2{e2}, 'ok', 'linestyle', 'none', 'markersize', 7, 'LineWidth', 2);
            
            for e1=1:length(indices2{e2});
                if mod(e1, 2) > 0
                    signum1=1;
                    text((t_SP(2)-t_SP(1))*indices2{e2}(e1), 0.98*ylim1, num2str(e1), 'color', [0 0 0], 'Fontsize', 12, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', 'Interpreter', 'none');
                else
                    signum1=-1;
                end
            end
            
        end
    end
    
end
        
% add the lines around the perimeter of the plot
if length(h2) >= 1
	psuedo_box(h2);
end

if same_ylim == 1
    ylim2=per_mar*ceil( 10*(max(max(abs(vibs)))) );    
end


% plot the vibrations data
for e2=1:m2;
    
    if same_ylim ~= 1
    	ylim2=per_mar*ceil( 10*(max(max(abs(vibs(e2, :))))) );   
    end

    if e2==1;
    	if m1 < 1
        	subaxis(nn, 1, 1, 'sh', sh, 'sv', sv , 'pl', 0, 'pr', 0, 'pt', 0, 'pb', 0, 'ml', ml, 'mr', mr, 'mt', mt, 'mb', mb); 
        else
        	subaxis(nn, 1, m1+e2, 'sh', sh, 'sv', sv , 'pl', 0, 'pr', 0, 'pt', 0, 'pb', 0, 'ml', ml, 'mr', mr, 'mt', mt, 'mb', mb); 
        end
    else
    	subaxis(nn, 1, m1+e2, 'sh', sh, 'sv', sv , 'pl', 0, 'pr', 0, 'pt', 0, 'pb', 0, 'ml', ml, 'mr', mr, 'mt', mt, 'mb', mb); 
    end
    t_vibs_rs=[];
    vibs_rs=[];
    
    if flag_rs == 1
    	[t_vibs_rs, vibs_rs]=resample_plot(t_vibs, vibs(e2, :));
    	plot(t_vibs_rs, vibs_rs');
        clear('t_vibs_rs');
        clear('vibs_rs');
    else
    	plot(t_vibs, vibs(e2, :)');
	end
    h2=[h2 gca];   
    
    if e2 == floor((m2+1)/2)
        ylabel('Vibs (m/s^2)', 'Fontsize', 16);
    end
    
    hold on;
    if m1+e2 == 1
        title( [Tool_Name, ' Filename ', filename1], 'Interpreter', 'none', 'Fontsize', 15 );
    end
    
    ylim(ylim2*[-1 1]);
    xlim([min_t max_t]);
    set(gca, 'Fontsize', 12, 'box', 'off' );
    if m1+e2 ~= last_x_axis
        set(gca, 'xtick', [], 'XTickLabel', '');
    end
    
    [ytick_m, YTickLabel1, ytick_good, ytick_new, yticklabel_new]=fix_YTick(0);
                
    wb_th=text( max_t-0.02*(max_t-min_t), -0.98*ylim2, ['Accel Channel ', num2str(e2), ' ', plot_str{m1+e2}], 'Fontsize', 10, 'Color', [0 0 0], 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom', 'BackgroundColor', 'none', 'Interpreter', 'none' );
    wb_th_array(m1+e2)=wb_th;
    
    if nargin >= 8 

        if max(size(indices2)) > e2 && max(size(SP_local_max2)) > e2
            plot((t_vibs(2)-t_vibs(1))*indices2{e2}, SP_local_max2{e2}, 'ok', 'linestyle', 'none', 'markersize', 7);


            for e1=1:length(indices2{e2});
                if mod(e1, 2) > 0
                    signum1=1;
                    text((t_SP(2)-t_SP(1))*indices2{e2}(e1), 0.98*ylim2, num2str(e1), 'color', [0 0 0], 'Fontsize', 12, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', 'Interpreter', 'none');
                else
                    signum1=-1;
                end
            end
        end
    end
    
end
     
% add the lines around the perimeter of the plot
if length(h2(m1+[1:m2])) >= 1
    psuedo_box(h2(m1+[1:m2]));
end

% set the x_label for the plot
xlabel('Time (seconds)', 'Fontsize', 16);


⌨️ 快捷键说明

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