stalta.c 4.26 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
Tilman Steinweg's avatar
Tilman Steinweg committed
3
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
4
 * This file is part of IFOS.
Tilman Steinweg's avatar
Tilman Steinweg committed
5
 * 
Florian Wittkamp's avatar
Florian Wittkamp committed
6
 * IFOS is free software: you can redistribute it and/or modify
Tilman Steinweg's avatar
Tilman Steinweg committed
7 8 9
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
Florian Wittkamp's avatar
Florian Wittkamp committed
10
 * IFOS is distributed in the hope that it will be useful,
Tilman Steinweg's avatar
Tilman Steinweg committed
11 12 13 14 15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
Florian Wittkamp's avatar
Florian Wittkamp committed
16
 * along with IFOS. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
Tilman Steinweg's avatar
Tilman Steinweg committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
-----------------------------------------------------------------------------------------*/

/*
 *------------------------------------------------------------------------
 *
 *   STA/LTA first arrival time picker
 *
 *------------------------------------------------------------------------
*/


#include "fd.h"

/* subroutine prototypes */
float mean(float *vec, int istart, int iend);
float maximumpos(float *a, int n);


void stalta(float **sectionp, int ntr, int nst, float *picked_times)
{
	
	extern float	DT;
	extern int POS[3];

	int	stawin;		/* sta time window in samples			*/
	int	ltawin;		/* lta time window in samples			*/
	float	*sta;		/* average of samples over window stawin	*/
	float	*lta;		/* average of samples over window ltawin	*/
	float	*stalta;		/* sta/lta					*/
	int	staltawin;		/* search max(sta/lta) in (1,...,staltawin)	*/
	int	maxpos=1;		/* position of maximum in stalta		*/
	float	*stmp, *ltmp;	/* temporary variables			*/
	int	j,k,i;
	float	*data, maxdata;
	char    jac[225];
        FILE    *fp1;

	/* define time windows */
        stawin    = 10;	 /*10;*/
	ltawin    = 30;	 /*30;*/
	staltawin = nst; /*500;*/
	
	
	/* allocate memory for vector variables */
	lta = vector(1,nst);
	sta = vector(1,nst);
	ltmp = vector(1,ltawin);
	stmp = vector(1,stawin);
	stalta = vector(1,nst);
	data = vector(1,nst);

        sprintf(jac,"STA_LTA_times.%i.%i",POS[1],POS[2]);
        fp1=fopen(jac,"w");


	/* Main loop over traces */
	for(i=1;i<=ntr;i++) {
		/*****************/
		/* extract trace */
		/*****************/
		for (j=1;j<=nst;j++) {
			if (j==1) {
				data[1] = 0.0;
				continue;
			}
			data[j] = sectionp[i][j];
			if (j==2)  maxdata = data[j];
			else { if (maxdata<data[j]) maxdata = data[j]; }
		}
		
				
		/***********/
		/* STA/LTA */
		/***********/
		/* square of trace data */
		for (j=1;j<=nst;j++)  data[j] = data[j]*data[j]/(maxdata*maxdata);
		
		/* apply STA/LTA-algorithm to trace */
		for (j=1;j<=nst-ltawin;j++) {
			/* copy trace piece to temporary variable */
			for (k=0;k<=ltawin-1;k++) ltmp[k] = data[j+k];
			/* average */			
			lta[j+ltawin-1] = mean(ltmp, 1, ltawin);
		}
		for (j=1;j<=nst-stawin;j++) {
			/* copy trace piece to temporary variable */
			for (k=0;k<=stawin-1;k++) stmp[k] = data[j+k];
			/* average */			
			sta[j+stawin-1] = mean(stmp, 1, stawin);
		}
		for (j=1;j<=nst;j++) {
			if (lta[j] == 0 || sta[j] == 0)
				stalta[j] = -1; /*setnan(stalta[j]);*/
			else
				stalta[j] = sta[j]/lta[j];
		}
		maxpos = maximumpos(stalta, staltawin);
		maxpos = maxpos - (int)(((float)(ltawin-stawin))/2);
		picked_times[i] = (float)maxpos*DT;
		
		/* output of picked times to file */
		fprintf(fp1,"%d \t %e \n",i,picked_times[i]);
	}
	
	fclose(fp1);
	
	free_vector(lta,1,nst);
	free_vector(sta,1,nst);
	free_vector(ltmp,1,ltawin);
	free_vector(stmp,1,stawin);
	free_vector(stalta,1,nst);
	free_vector(data,1,nst);

}


/*========================================================================*/
/*                              SUBROUTINES                               */
/*========================================================================*/

float mean(float *vec, int istart, int iend)
{
	int i;
	float m=0;
	
	for (i=istart;i<=iend;i++) m += vec[i];
	m /= (iend-istart+1);
	
	return m;
}


float maximumpos(float *a, int n){
	float maxi=0.0;
	int j;
	int maxpos = 1;

	for (j=1;j<=n;j++)
	{	
		/* if (isnan(a[j])) continue; */
		/* if (!finite(a[j])) continue; */
		if (a[j]<0) continue;
		if (fabs(a[j])>maxi)
		{
			maxi = fabs(a[j]);
			maxpos = j;
		}
	}
		
	return maxpos;
}