📄 harmon_new.m,v
字号:
head 3.0;access;symbols;locks; strict;comment @// @;3.0date 2000.06.13.19.20.30; author gilles; state Exp;branches;next 2.0;2.0date 99.05.21.18.45.46; author mah; state Exp;branches;next 1.10;1.10date 99.02.10.20.47.00; author mah; state Exp;branches;next 1.9;1.9date 99.02.10.20.11.36; author perron; state Exp;branches;next 1.8;1.8date 99.02.10.20.07.19; author perron; state Exp;branches;next 1.7;1.7date 99.02.10.20.04.11; author perron; state Exp;branches;next 1.6;1.6date 99.02.10.19.52.34; author perron; state Exp;branches;next 1.5;1.5date 99.02.10.16.15.19; author eadam; state Exp;branches;next 1.4;1.4date 99.02.10.13.50.49; author mah; state Exp;branches;next 1.3;1.3date 99.02.02.15.55.12; author mah; state Exp;branches;next 1.2;1.2date 99.02.02.15.07.07; author mah; state Exp;branches;next 1.1;1.1date 99.02.02.14.20.47; author mah; state Exp;branches;next ;desc@modified harmon to use a specified time window to calculate the corrections@3.0log@Release 3@text@function [dataout] = harmon_new(datain,freq1,freq2,inter,t1,t2)% function [dataout] = harmon_new(datain,freq1,freq2,inter,t1,t2)%% Harmon is a time domain notch filter. Discrete Fourier% Transforms are calculated to estimate noise phase and amplitude.% The estimated noise is subtracted from the trace in the time domain.% It has been rewritten to analyze a window of data from t1 to t2% and then to apply it to the whole trace%% Usage:%% Input:% datain : Input data% freq1 : Start scanning at this frequency (Hz)% freq2 : Upper frequency limit (Hz)% inter : Frequency step (Hz) such that ((freq2-freq1)/inter) is% a multiple of 10% t1 : Start time of window (seconds)% t2 : End time of window (seconds)%% Output:% dataout : Output data%% Example :% This is what I use to remove 60 Hz noise% filtered = harmon_new(data,59.88,60.12,0.01,0,0.5);%% Reference:% http://www.cg.NRCan.gc.ca/staff/adam/software/monofreq.html%% Warning: Program does not check for reasonable parameters%%written by: Erick ADAM%Dec. 1995%Rewritten by Marko Mah February 1999%$Id: harmon_new.m,v 2.0 1999/05/21 18:45:46 mah Exp gilles $%$Log: harmon_new.m,v $%Revision 2.0 1999/05/21 18:45:46 mah%Release 2%%Revision 1.10 1999/02/10 20:47:00 mah%fixing up bugs previous programmer(s) put in%%Revision 1.9 1999/02/10 20:11:36 perron%*** empty log message ***%%Revision 1.8 1999/02/10 20:07:19 perron%modifying if statement at line 119%%Revision 1.7 1999/02/10 20:04:11 perron%Modifying if statement at line 119%%Revision 1.6 1999/02/10 19:52:34 perron%Modifying help text%%Revision 1.5 1999/02/10 16:15:19 eadam%Optimisation by iterative windowing fro finding the maximum%%Revision 1.4 1999/02/10 13:50:49 mah%fixed bug%%Revision 1.3 1999/02/02 15:55:12 mah%corrected amplitude of correction%%Revision 1.2 1999/02/02 15:07:07 mah%fixed a bug%%Revision 1.1 1999/02/02 14:20:47 mah%Initial revision%%%%Copyright (C) 1998 Seismology and Electromagnetic Section/%Continental Geosciences Division/Geological Survey of Canada%%This library is free software; you can redistribute it and/or%modify it under the terms of the GNU Library General Public%License as published by the Free Software Foundation; either%version 2 of the License, or (at your option) any later version.%%This library is distributed in the hope that it will be useful,%but WITHOUT ANY WARRANTY; without even the implied warranty of%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU%Library General Public License for more details.%%You should have received a copy of the GNU Library General Public%License along with this library; if not, write to the%Free Software Foundation, Inc., 59 Temple Place - Suite 330,%Boston, MA 02111-1307, USA.%%DSI Consortium%Continental Geosciences Division%Geological Survey of Canada%615 Booth St.%Ottawa, Ontario%K1A 0E9%%email: dsi@@cg.nrcan.gc.cadisp('[dataout] = harmon_new(datain,freq1,freq2,inter,t1,t2)')deltat=datain.fh{8}; %sampling interval in secondsfreq = freq1:inter:freq2; %Vector of frequencies for DFTfreqlen = length(freq); %Number of frequencies for DFT samples = datain.fh{7}; %number of points per trace;% the following converts t1 and t2 to pointst1pt=round(t1/deltat)+1;t2pt=round(t2/deltat)+1;%Initialize local variables for speedupcos_wt = ones(samples,freqlen); % Cosine lookup tablesin_wt = ones(samples,freqlen); % Sine lookup tabledataout = datain; % Output variablesamp = (0.0 : deltat : (samples-1)*deltat)'; % Time vector% Optimization : I create a lookup table for Sine and cosine.for i = 1 : freqlen, w = 6.283185307 * freq(i); % Convert frequency to radian (w = 2*PI*f) cos_wt(:,i)=cos(samp*w); % Calculate cos(w*t) and store in lookup table sin_wt(:,i)=sin(samp*w); % Calculate sin(w*t) and store in lookup tableend%step = 10; % Calculate amplitudes from DFT at each frequencies of vector freqfor COUNT=1:datain.fh{12} % Loop over over number of records for tr=1:datain.th{COUNT}(12,1) % loop over traces step = floor(freqlen/10); % Speedup to calculate DFT for every 10th frequencies large= 0.0; % Maximum amplitude (reset to 0 before each scan) trace = datain.dat{COUNT}(t1pt:t2pt,tr); start_freq = 1; % start at the first frequency end_freq = freqlen; % End at the last frequency if step >= 1 % loop until every frequency is read for j = start_freq:step:end_freq, % Loop over frequency range suma = sum(trace.*cos_wt(t1pt:t2pt,j))* 2.0 /samples; sumb = sum(trace.*sin_wt(t1pt:t2pt,j))* 2.0 /samples; amp_max = (suma*suma)+(sumb*sumb); %Amplitude for freq(j) if amp_max > large %if Amplitude freq(j) is larger than previous ones freqi=j; %record the index of the maximum amplitude large = amp_max; % reset large a_max=suma; %record factor A of the DFT b_max=sumb; %record factor B of the DFT end % End if statement end % loop over frequency start_freq = (freqi-2)*step; % New frequency range as a function of the maximum amplitude end_freq = (freqi-2)*step; step = floor(step/10); % reduce the step by a factor of 10 end % End if statement % the following corrects a_max and b_max because a smaller window was used to calculate them a_max=a_max*samples/(t2pt-t1pt+1); b_max=b_max*samples/(t2pt-t1pt+1); % The filter is applied here dataout.dat{COUNT}(:,tr) = datain.dat{COUNT}(:,tr)-((a_max*cos_wt(:,freqi))+(b_max * sin_wt(:,freqi))); end % loop over tracesend % loop over records@2.0log@Release 2@text@d38 1a38 1%$Id: harmon_new.m,v 1.10 1999/02/10 20:47:00 mah Exp mah $d40 3@1.10log@fixing up bugs previous programmer(s) put in@text@d38 1a38 1%$Id: harmon_new.m,v 1.9 1999/02/10 20:11:36 perron Exp $d40 3@1.9log@*** empty log message ***@text@d38 1a38 1%$Id: harmon_new.m,v 1.8 1999/02/10 20:07:19 perron Exp perron $d40 3d118 1a118 1step = 10; d123 1a123 1 step = floor(freqlen/10); % Speedup : calculate DFT for every 10th frequencies d128 4a131 2 if (step => 1) % loop until every frequency is read for j = start_freq: step:end_freq, % Loop over frequency range d135 2a136 1 if(amp_max > large) %if Amplitude freq(j) is larger than previous onesd142 1d144 1@1.8log@modifying if statement at line 119@text@d38 1a38 1%$Id: harmon_new.m,v 1.7 1999/02/10 20:04:11 perron Exp perron $d40 3d125 1a125 1 if (step => 1) % loop until every frequency is read@1.7log@Modifying if statement at line 119@text@d38 1a38 1%$Id: harmon_new.m,v 1.6 1999/02/10 19:52:34 perron Exp perron $d40 3d122 1a122 1 if step >= 1 % loop until every frequency is read@1.6log@Modifying help text@text@d38 1a38 1%$Id: harmon_new.m,v 1.5 1999/02/10 16:15:19 eadam Exp perron $d40 3d119 1a119 1 if step => 1 % loop until every frequency is read@1.5log@Optimisation by iterative windowing fro finding the maximum@text@d17 1a17 1 a multiple of 10d38 1a38 1%$Id: harmon_new.m,v 1.4 1999/02/10 13:50:49 mah Exp eadam $d40 3@1.4log@fixed bug@text@d16 2a17 1% inter : Frequency step (Hz)d38 1a38 1%$Id: harmon_new.m,v 1.3 1999/02/02 15:55:12 mah Exp mah $d40 3d84 1a84 1freqlen = length(freq); %Number of frequencies for DFTd103 2d108 1d111 18a128 11 for j = 1: freqlen, % Loop over frequencies suma = sum(trace.*cos_wt(t1pt:t2pt,j))* 2.0 /samples; sumb = sum(trace.*sin_wt(t1pt:t2pt,j))* 2.0 /samples; amp_max = (suma*suma)+(sumb*sumb); %Amplitude for freq(j) if(amp_max > large) %if Amplitude freq(j) is larger than previous ones freqi=j; %record the index of the maximum amplitude large = amp_max; % reset large a_max=suma; %record factor A of the DFT b_max=sumb; %record factor B of the DFT end % End if statement end % loop over frequency@1.3log@corrected amplitude of correction@text@d37 1a37 1%$Id: harmon_new.m,v 1.2 1999/02/02 15:07:07 mah Exp mah $d39 3d80 1a80 1freqs = length(freq); %Number of frequencies for DFTd87 2a88 2cos_wt = ones(samples,freqs); % Cosine lookup tablesin_wt = ones(samples,freqs); % Sine lookup tabled93 1a93 1for i = 1 : freqs,d104 6a109 6 for j = 1: freqs, % Loop over frequencies suma = sum(trace.* cos_wt(t1pt:t2pt,j))* 2.0 /samples; sumb = sum(trace.* sin_wt(t1pt:t2pt,j))* 2.0 /samples; amp_max = (suma * suma) + (sumb * sumb); % Amplitude for freq(j) if(amp_max > large) % if Amplitude freq(j) is larger than previous ones freqi=j; % record the index of the maximum amplitude d111 2a112 2 a_max=suma; % record factor A of the DFT b_max=sumb; % record factor B of the DFT@1.2log@fixed a bug@text@d37 1a37 1%$Id: harmon_new.m,v 1.1 1999/02/02 14:20:47 mah Exp mah $d39 3d112 3@1.1log@Initial revision@text@d30 2d37 4a40 2%$Id: Exp $%$Log: $d43 1d78 1a78 1t2pt=rount(t2/deltat)+1;@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -