Package: fast-konno-ohmachi
FastKonno-OhmachiA Python library that performs Konno-Ohmachi spectral smoothing very fast (2x speedup compared to the "vanilla" Konno-Ohmachi smoothing algorithm).BackgroundKonno-Ohmachi is a smoothing algorithm proposed by Konno & Ohmachi (1998) [abstract,PDF], which achieves a "uniform-span" smoothing to frequency spectra in the logarithmic scale.For lower frequencies, the Konno-Ohmachi smoothing window is narrower (i.e., less smoothing), and for higher frequencies, the window is wider (i.e., more smoothing).This makes the Konno-Ohmachi filter particularly appealing to seismologists, who often try to avoid over-smoothing lower frequencies (< 10 Hz) of seismic wave signals.The plot below shows the result of Konno-Ohmachi filter versus a regularmedian value filter. The two filters yield similar results for frequency > 5 Hz, but for lower frequencies, the median filter over-smoothes the original signal, which is undesirable.(The raw signal used in this example is the Fourier amplitude spectrum of a ground acceleration waveform recorded during theMagnitude-9.0 Tohoku-Oki Earthquake on March 11, 2011.)Computation speedConventionally, Konno-Ohmachi filtering is time-consuming because its smoothing windows are different at each frequency and need to be calculated one by one.This library achieves a 2x speedup by pre-calculating smoothing windows (i.e., trading memory space for speed).It can speed up calculation even further by performing parallel computing (thefaster_konno_ohmachi()function).The only minor compromises in order to achieve the 2x speedup are:Only even integer smoothing strengths from 2 to 100 are supported. This is not an issue in reality because people rarely need non-integer smoothing strengths.The smoothing results fromfast_konno_ohmachi()andfaster_konno_ohmachi()are not entirely identical to the smoothing result fromslow_konno_ohmachi()(the "vanilla" algorithm). However, the differences are too minor to have any practical implications.Installationpipinstallfast-konno-ohmachiUsageimportnumpyasnpimportfast_konno_ohmachiasfkofreq=np.arange(0,2,0.1)# just an arbitrary examplesignal=np.sin(freq)smoothed=fko.fast_konno_ohmachi(signal,freq,smooth_coeff=40,progress_bar=True)or (calculate in parallel)smoothed=fko.faster_konno_ohmachi(signal,freq,smooth_coeff=40,n_cores=4)or (if you'd like to see how slow the "vanilla" implementation can be)smoothed=fko.slow_konno_ohmachi(signal,freq,smooth_coeff=40,progress_bar=True)You can also try to rundemo/Demo_konno_ohmachi_smooth.pyto smooth a real-world signal. (You'd needscipyandmatplotlibto run the demo script.)Additional notes on parallel computingWhen usingfaster_konno_ohmachi(), the user should to protect the main script withif __name__ == '__main__'(see the demo script). This ismandatoryfor Windows, andhighly recommendedfor Mac/Linux.Thefaster_konno_ohmachi()function uses multiple CPU cores, but it is not necessarily faster thanfast_konno_ohmachi(), because the data I/O between the CPU cores takes extra time ("computation overhead"). Below is a benchmarking of the running time for input signals with different length:Length of  signal (x1000)135791113151719212325272931Time of "fast" (sec)0.10.40.91.52.23.34.45.46.88.19.611.213.114.916.718.8Time of "faster" (sec)2.42.42.83.03.33.94.14.64.95.55.86.77.58.29.09.8Or as shown in this figure: