49

MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Embed Size (px)

Citation preview

Page 1: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header
Page 2: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

MATLABSAC

Page 3: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Data Format and HeaderEach signal or seismogram is stored in a

separate binary or alphanumeric data file.

Each data file contains a header that describes the contents of that file.

Page 4: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Time The SAC header contains a reference or zero time,

stored as six integers (NZYEAR, NZJDAY, NZHOUR, NZMIN, NZSEC, NZMSEC), but normally printed in an equivalent alphanumeric format (KZDATE and KZTIME).

%sac

SAC> r GRAT.EHZ.NM

SAC> lh #list header

FILE: GRAT.EHZ.NM – 1 #this is a partial header

-----------------

NPTS = 360100

B = 0.000000e+00

E = 3.600990e+03

IFTYPE = TIME SERIES FILE

DELTA = 1.000000e-02

KZDATE = APR 06 (097), 2008

KZTIME = 02:59:59.320

Page 5: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Event and Station Info SAC header can store station and event info

FILE: WMQ.BHZ.D.1995.073:10.34.49 - 1

---------------------------------

KSTNM = WMQ

STLA = 4.382110e+01

STLO = 8.769500e+01

STEL = 8.970000e+02 (m)

EVLA = 3.086000e+00

EVLO = 9.584800e+01

EVDP = 3.040000e+01

Page 6: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Once the event and station information are store, the program automatically calculates and stores distance, azimuth, backazimuth, and great circle arc length

DIST = 4.583862e+03 (km)

AZ = 3.510350e+02 (degrees)

BAZ = 1.675856e+02 (degrees)

GCARC = 4.120298e+01 (degrees)

Page 8: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Matlab function: sac.mfunction [head1, head2, head3, data]=sac(filename)

% Read one single SAC format file

%head1 (float matrix), head2 (int matrix), and head3 (char matrix).

% head3 here is integer matrix, to show it under ASCII, just char(head3).

fid=fopen(filename, 'rb','b');

status=fseek(fid,0,'bof');

head1=fread(fid, [5, 14], 'float32');

head2=fread(fid, [5, 8], 'int32');

head3=fread(fid, [24, 8], 'char');

head1=head1'; head2=head2'; head3=head3';

data=fread(fid, npts, 'float32');

Page 9: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Matlab function: loadsac.m

function [data,npts,delta,b,dist,az,baz,gcarc] = …

loadsac(sacfilename)

[head1, head2, head3, data]= …

sac(sacfilename);

delta = head1(1);

npts=head2(2,5);

b = head1(2,1);

dist= head1(11,1);

az = head1(11,2);

baz = head1(11,3);

gcarc=head1(11,4);

As currently written, you modifythe output of loadsac to get the header values you are interested in …. Needs to be generalized, and other similar sac readers exist

Page 10: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Coding and Flow ChartsLonger codes may require a roadmap

(much like your research projects!)

Today’s lecture will focus on understanding Chuck’s matlab script for polarization analysis using 3 component recordings of body and/or surface waves

http://www.ceri.memphis.edu/people/langston/matlab/polarize.html

Page 11: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

GOAL: solve for polarization using 3 component seismic data

Starting data: 3 component single station SAC formatted data

Result: Identify the azimuth(s) of the primary wave(s) recorded in the data

How to we get from A to B?

Page 12: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Principal Component Analysis

• Principal component analysis (PCA) is a vector space transform often used to reduce multidimensional data sets to lower dimensions for analysis.

Page 13: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

• PCA involves the calculation of the eigenvalue decomposition of a data covariance matrix or singular value decomposition of a data matrix, usually after mean centering the data for each attribute.

• Its operation can be thought of as revealing the internal structure of the data in a way which best explains the variance in the data.

Page 14: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

What we are looking for:

• New set of axes (basis) that maximizes the correlation of HT(Z) with R, and minimizes the correlations between both HT(Z) and R with T.

• We are not using the full power of PCA, since we already have some model for the result of the analysis (and have therefore preprocessed the data by taking the Hilbert transform of the z component).

Page 15: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

What is the idea?

• Seismic waves are polarized

• P wave longitudinal (V and R)

• S wave transverse with SH and SV polarizations (T, V and R)

• Rayleigh waves (V and R)

• Love waves (T).

Page 16: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

GOAL: Solve for polarization

Page 17: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Step 1: Data Preparation

Page 18: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Create function ‘polarize’function polarize(station,twin,hilb,flp,fhi)

% Program to read in 3 component waveform data

% Create the covariance matrix for a moving time window

% Find the principal components and infer polarization

% Written by CL Langston on somedate

% input:

% station = station name for sacfile prefix

% twin = window size in secs

% hilb=1 to preform hilbert transform, 0 to not

% flp = low frequency corner frequency of a 2nd order butterworth

% filter used to filter the data, if 0, then no filtering

% fhi = hi frequency corner frequency of the filter

Page 19: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Loading SAC data Many matlab scripts exist to read in sac data.

Chuck provides one .m file called readsac.m online Currently this is sensitive to byte order format and requires that

you provide the npts and delta of the input sac data… It is not as flexible as it could be Amplitude data is a row vector

I have one loadsac.m that uses a subroutine ‘sac’ not sensitive to byte order returns the data, npts, delta, and begin point of the SAC file data is a column vector may be stolen from /home/hdeshon/Bolivia/matlab

Page 20: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

%use input variable station to construct the filenames

ename=strcat(station,'.BHE.SAC');

nname=strcat(station,'.BHN.SAC');

zname=strcat(station,'.BHZ.SAC');

[e,npts1,delt1,b] = loadsac(ename);

[n,npts2,delt2,b] = loadsac(nname);

[z,npts3,delt3,b] = loadsac(zname);

Page 21: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

if npts1~=npts2 && npts1~=npts3 %requires data be same length

exit;

else

npts=npts1;

delt=delt1;

ttot=(npts-1)*delt; %total time in secs

for i=1:npts

t(1,i)=i*delt; %create row vector of times rather than points

end

endHow would we vectorized this?

t=linspace(1,npts,npts);t=t*delt;

Page 22: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Removing the data meanWe need to remove the mean of the data for

principal component analysis (PCA).

We also need to transpose the column vector data into row vectors.

e=dmean(e’); % remove the mean from each

n=dmean(n’); % and transpose the data

z=dmean(z’);

Page 23: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

subroutine: dmeanfunction [a]=dmean(b)

% [a]=dmean(b)

% Remove the mean from a row vector

m=mean(b);

a=b-m;

return;

Page 24: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

For this example, the data is actually a synthetically derived set of surface waves using a function Chuck wrote called testchirps. Real data would not look this clean.

Page 25: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Rayleigh R and Z related by Hilbert X-form

90° phase shift, blue trace is Hilbert Transformed to green trace, then overlays red trace

Page 26: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Hilbert Transformif hilb ==1; % hilbert transform the vertical component

zh=hilbert(z); % to make Rayleigh wave in phase on vert and horz

z=-imag(zh); % if present

else;

end;

Page 27: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Make Love and Rayleigh waves (Z, R and T)

Page 28: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Rotate horizontals into seismograms @ 30°.

Page 29: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Plot the data% plot the raw data

f1=figure('name','DATA SEISMOGRAMS');

subplot(3,1,1); plot(t,e);

xlabel('time sec');

ylabel(strcat('EW Comp at ',station));

subplot(3,1,2); plot(t,n);

xlabel('time sec');

ylabel(strcat('NS Comp at ',station));

subplot(3,1,3); plot(t,z);

xlabel('time sec');

ylabel(strcat('Z comp at ',station));

Love Wave

Rayleigh Wave

Rayleigh Wave

Page 30: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

FilteringFiltering is a two part project in Matlab

Design the filterApply the filter

There is a filter design GUI you can use to design the perfect filter called fdatool

Or you can design filters using pre-built filter types (Butterworth, Bessel, etc.)

Page 31: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

function [d]=bandpass(c,flp,fhi,npts,delt)

% [d]=bandpass(c,flp)

% bandpass a time series with a 2nd order butterworth filter

% c = input time series

% flp = lowpass corner frequency of filter

% fhi = highpass corner frequency

% npts = samples in data

% delt = sampling interval of data

n=2; % 2nd order butterworth filter

fnq=1/(2*delt); % Nyquist frequency

Wn=[flp/fnq fhi/fnq]; % non-dimensionalize the corner frequencies

[b,a]=butter(n,Wn); % butterworth bandpass non-dimensional frequency

d=filtfilt(b,a,c); % apply the filter: use zero phase filter (p=2)

return;

Page 32: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Filter & plot the filtered data

% filter the data

e1=bandpass(e,flp,fhi,npts,delt);

n1=bandpass(n,flp,fhi,npts,delt);

z1=bandpass(z,flp,fhi,npts,delt);

e=e1; n=n1; z=z1;

% plot the filtered data

f2=figure('name','FILTERED SEISMOGRAMS');

subplot(3,1,1); plot(t,e1);

…… removed for clarity

Page 33: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

GOAL: Solve for polarization

Page 34: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Step 2: Main Code

Page 35: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Moving window using loops

% Moving window loop

npts1=fix(ttot/delt) + 1; % total number of samples to analyze

nwin=fix(twin/delt) + 1; % number of samples in a time window

npshift=fix(twin/(2*delt))+1; % number of samples to shift over

kfin=fix((npts1-nwin)/(npshift+1))+1; % number of time windows considered

mxde1=0.;

mxde2=0.;

mxde3=0.;

npts1nwin

npshift

Page 36: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

for k=1:kfin;

nwinst=(k-1)*(npshift-1)+1; % start of time window

nwinfn=nwinst+nwin-1; % end of time window

…….. missing code to be supplied later

t2(k)=delt*(nwinst-1); % assign time for this window to the window start

end;k=1

k=2

k=3

k=kfin

Page 37: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Eigenvalues/Eigenvectors

Page 38: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header
Page 39: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Missing code from inside our loop

a=csigm(e,n,z,nwinst,nwinfn); % signal matrix

c=a'*a; % covariance matrix

[v1,d1]=eig(c); % eigenvalue/eigenvectors

[v,d]=order(v1,d1); % put eigenvalues & eigenvectors in ascending order

% azimuth for each of the 3 eigenvalues

ang1(k)=atan2(v(1,1),v(2,1)) * 180/pi;

ang2(k)=atan2(v(1,2),v(2,2)) * 180/pi;

ang3(k)=atan2(v(1,3),v(2,3)) * 180/pi;

% incidence angle of the 3 eigenvalues

vang1(k)=acos(abs(v(3,1)))* 180/pi; %angle from the vertical

vang2(k)=acos(abs(v(3,2)))* 180/pi;

vang3(k)=acos(abs(v(3,3)))* 180/pi;

Page 40: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Still in loop

de1(k)=d(1);

de2(k)=d(2);

de3(k)=d(3);

mxde1=max(mxde1,de1(k)); % find the maximum values

mxde2=max(mxde2,de2(k));

mxde3=max(mxde3,de3(k));

Page 41: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Outside of Loop again

f3=figure('name','Eigenvalues and Inferred Azimuth');

subplot(3,1,1); plot(t2,de1,'-or',t2,de2,'-dg',t2,de3,'-+b');

xlabel('time sec');

ylabel('eigenvalues');

subplot(3,1,2); plot(t2,ang1,'-or',t2,ang2,'-dg',t2,ang3,'-+b');

xlabel('time sec');

ylabel('Azimuth ');

subplot(3,1,3); plot(t2,vang1,'-or',t2,vang2,'-dg',t2,vang3,'-+b');

xlabel('time sec');

ylabel('incidence angle ');

Page 42: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header
Page 43: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

GOAL: Solve for polarization

Page 44: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

Rose Diagrams% Rose plots

f4=figure('name','Azimuth Distribution');

subplot(2,3,1);

title('Azimuth - Largest Eigenvalue');

rose(ang1*pi/180,100);

subplot(2,3,2);

title('Azimuth - Intermediate Eigenvalue');

rose(ang2*pi/180,100);

subplot(2,3,3);

title('Azimuth - Smallest Eigenvalue');

rose(ang3*pi/180,100);

Page 45: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header
Page 46: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

• If we take a short time period we can think of each component as a vector of n terms.

• If we take the dot product of each vector with itself and with the other two components we can find the “angle” between them.

2 Bonus Points: Vectorization of polarization code

Page 47: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

• We can also make these dot products by making a 3xn array using each seismogram as a row.

• Multiplying this array with its transpose results in a 3x3 matrix with the various dot products in the elements of the matrix.

Page 48: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

• Now find the eigenvectors and eigenvalues of this matrix.

• From the eigenvectors we can make a rotation matrix that will rotate our matrix to a diagonal matrix.

• The off diagonal elements are now all zero and from the geometric interpretation of the dot product this means that the two vectors used to make that dot product are perpendicular.

Page 49: MATLAB SAC Data Format and Header Each signal or seismogram is stored in a separate binary or alphanumeric data file. Each data file contains a header

• So we can rotate the original horizontal components into a new set of seismograms rotated to the principal directions defined by the eigenvectors.

• The dot products of the off diagonal terms will now be zero, indicating the vectors are perpendicular.