| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2012 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
! |
| 24 |
! contains subroutines for taking power spectrums on particle data |
| 25 |
! |
| 26 |
module powerspectrums |
| 27 |
implicit none |
| 28 |
real, parameter, private :: pi = 4.*atan(1.) |
| 29 |
real, parameter, private :: twopi = 2.*pi |
| 30 |
public :: powerspectrum |
| 31 |
|
| 32 |
private |
| 33 |
|
| 34 |
contains |
| 35 |
|
| 36 |
subroutine powerspectrum(npts,x,dat,nfreqpts,freq,power,idisordered) |
| 37 |
integer, intent(in) :: npts, nfreqpts |
| 38 |
real, intent(in), dimension(npts) :: x |
| 39 |
real, intent(in), dimension(npts) :: dat |
| 40 |
real, intent(in), dimension(nfreqpts) :: freq |
| 41 |
real, intent(out), dimension(nfreqpts) :: power |
| 42 |
logical, intent(in) :: idisordered |
| 43 |
integer :: ifreq |
| 44 |
real :: datmean, datvar, omega |
| 45 |
|
| 46 |
if (.not.idisordered) then |
| 47 |
print*,' evaluating fourier transform' |
| 48 |
do ifreq=1,nfreqpts |
| 49 |
omega = twopi*freq(ifreq) |
| 50 |
!--get power at this frequency |
| 51 |
call power_fourier(npts,x,dat,omega,power(ifreq)) |
| 52 |
enddo |
| 53 |
else |
| 54 |
print*,'evaluating lomb periodogram...' |
| 55 |
! |
| 56 |
!--calculate the mean and variance of the data |
| 57 |
! |
| 58 |
call mean_variance(dat,npts,datmean,datvar) |
| 59 |
print*,'data mean = ',datmean,' std. dev = ',sqrt(datvar) |
| 60 |
if (datvar <= 0.) then |
| 61 |
print*,'error: variance = 0' |
| 62 |
power = 0. |
| 63 |
return |
| 64 |
endif |
| 65 |
do ifreq=1,nfreqpts |
| 66 |
omega = twopi*freq(ifreq) |
| 67 |
call power_lomb(npts,x,dat,datmean,datvar,omega,power(ifreq)) |
| 68 |
enddo |
| 69 |
|
| 70 |
endif |
| 71 |
|
| 72 |
end subroutine powerspectrum |
| 73 |
|
| 74 |
!------------------------------------------------------- |
| 75 |
! subroutine to compute the power spectrum |
| 76 |
! of evenly sampled data via a (slow) fourier transform |
| 77 |
!-------------------------------------------------------- |
| 78 |
|
| 79 |
subroutine power_fourier(npts,x,dat,omega,power) |
| 80 |
integer, intent(in) :: npts |
| 81 |
real, intent(in), dimension(npts) :: x, dat |
| 82 |
real, intent(in) :: omega |
| 83 |
real, intent(out) :: power |
| 84 |
integer :: i |
| 85 |
real :: sum1,sum2 |
| 86 |
|
| 87 |
power = 0. |
| 88 |
sum1 = 0. |
| 89 |
sum2 = 0. |
| 90 |
do i=1,npts |
| 91 |
sum1 = sum1 + dat(i)*cos(-omega*x(i)) |
| 92 |
sum2 = sum2 + dat(i)*sin(-omega*x(i)) |
| 93 |
enddo |
| 94 |
power= sqrt(sum1**2 + sum2**2)/REAL(npts) |
| 95 |
|
| 96 |
return |
| 97 |
end subroutine power_fourier |
| 98 |
|
| 99 |
!---------------------------------------------------------- |
| 100 |
! Subroutine to compute the power spectrum (periodogram) |
| 101 |
! of unevenly sampled data via the Lomb (1976) method |
| 102 |
! (algorithm described in Press et al, Numerical Recipes, sec 13.8, p569) |
| 103 |
! |
| 104 |
! Given the data (dat) on a set of points (x), |
| 105 |
! returns an array of nfreq frequencies (freq) between freqmin and freqmax |
| 106 |
! together with the power at each frequency (power) |
| 107 |
!---------------------------------------------------------- |
| 108 |
subroutine power_lomb(npts,x,dat,datmean,datvar,omega,power) |
| 109 |
integer, intent(in) :: npts |
| 110 |
real, intent(in), dimension(npts) :: x, dat |
| 111 |
real, intent(in) :: datmean,datvar,omega |
| 112 |
real, intent(out) :: power |
| 113 |
integer :: i |
| 114 |
real :: ddat |
| 115 |
real :: tau, tau_numerator, tau_denominator |
| 116 |
real :: term1_numerator, term1_denominator |
| 117 |
real :: term2_numerator, term2_denominator |
| 118 |
real :: omega_dx, cos_term, sin_term |
| 119 |
! |
| 120 |
!--calculate tau for this frequency |
| 121 |
! |
| 122 |
tau_numerator = 0. |
| 123 |
tau_denominator = 0. |
| 124 |
do i=1,npts |
| 125 |
tau_numerator = tau_numerator + sin(2.*omega*x(i)) |
| 126 |
tau_denominator = tau_denominator + cos(2.*omega*x(i)) |
| 127 |
enddo |
| 128 |
tau = atan(tau_numerator/tau_denominator)/(2.*omega) |
| 129 |
! |
| 130 |
!--calculate the terms in the power |
| 131 |
! |
| 132 |
term1_numerator = 0. |
| 133 |
term1_denominator = 0. |
| 134 |
term2_numerator = 0. |
| 135 |
term2_denominator = 0. |
| 136 |
do i=1,npts |
| 137 |
ddat = dat(i) - datmean |
| 138 |
omega_dx = omega*(x(i) - tau) |
| 139 |
cos_term = cos(omega_dx) |
| 140 |
sin_term = sin(omega_dx) |
| 141 |
term1_numerator = term1_numerator + ddat*cos_term |
| 142 |
term1_denominator = term1_denominator + cos_term**2 |
| 143 |
term2_numerator = term2_numerator + ddat*sin_term |
| 144 |
term2_denominator = term2_denominator + sin_term**2 |
| 145 |
enddo |
| 146 |
! |
| 147 |
!--calculate the power at this frequency |
| 148 |
! |
| 149 |
power = 1./(2.*datvar)*(term1_numerator**2/term1_denominator + & |
| 150 |
term2_numerator**2/term2_denominator) |
| 151 |
|
| 152 |
return |
| 153 |
end subroutine power_lomb |
| 154 |
|
| 155 |
!------------------------------------------------- |
| 156 |
! Subroutine to calculate the mean and variance |
| 157 |
! of a set of data points |
| 158 |
! Mean is trivial but variance uses a special |
| 159 |
! formula to reduce round-off error |
| 160 |
! see Press et al Numerical Recipes, section 14.2 |
| 161 |
! this is similar to their subroutine avevar |
| 162 |
!------------------------------------------------- |
| 163 |
subroutine mean_variance(x,npts,xmean,xvariance) |
| 164 |
integer, intent(in) :: npts |
| 165 |
real, intent(in), dimension(npts) :: x |
| 166 |
real, intent(out) :: xmean, xvariance |
| 167 |
real :: roundoff, delta |
| 168 |
integer :: i |
| 169 |
! |
| 170 |
!--calculate average |
| 171 |
! |
| 172 |
xmean = 0. |
| 173 |
do i=1,npts |
| 174 |
xmean = xmean + x(i) |
| 175 |
enddo |
| 176 |
xmean = xmean/real(npts) |
| 177 |
! |
| 178 |
!--calculate variance using the corrected two-pass formula |
| 179 |
! |
| 180 |
! var = 1/(n-1)*( sum (x-\bar{x}) - 1/n * (sum(x-\bar{x}) )^2 ) |
| 181 |
! |
| 182 |
! where the last term corrects for the roundoff error |
| 183 |
! in the first term |
| 184 |
! |
| 185 |
xvariance = 0. |
| 186 |
roundoff = 0. |
| 187 |
|
| 188 |
do i=1,npts |
| 189 |
delta = x(i) - xmean |
| 190 |
roundoff = roundoff + delta |
| 191 |
xvariance = xvariance + delta*delta |
| 192 |
enddo |
| 193 |
xvariance = (xvariance - roundoff**2/npts)/real(npts-1) |
| 194 |
|
| 195 |
return |
| 196 |
end subroutine mean_variance |
| 197 |
|
| 198 |
end module powerspectrums |