Skip to content

Commit

Permalink
Added documentation and switched fft libraries from numpy to scipy
Browse files Browse the repository at this point in the history
  • Loading branch information
RitvikPrabhu committed Jul 4, 2021
1 parent 26ddbac commit ac4c66c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
70 changes: 60 additions & 10 deletions pycc/rt/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from scipy.linalg import solve,toeplitz
from scipy.fftpack import fft,fftfreq
from scipy.fftpack import fft,fftfreq, ifft
import copy
from scipy.signal import chirp, find_peaks, peak_widths

Expand Down Expand Up @@ -49,38 +49,87 @@ def FT(data,dt=1,norm=False,n=None):

def denoise(f, filter_level, timestep):
"""
Denoise the signal in the time domain using FFT
Denoise a given signal in the time domain using fast fourier transform
Parameters
----------
f : np.ndarray
one-dimensional time-domain data
filter_level : float/int
level below which the values can be zeroed out
in the frequency domain
timestep : float/int
incremental change in the independent variable
Returns
-------
fifft : np.ndarray
denoised signal in the time domain
"""
# Use PS to filter out the noise
length = len(f) #Number of data points
x_array = np.arange(0, length)
fhat = np.fft.fft(f, length) #FFT
fhat = fft(f, length) #FFT
PS = fhat * np.conj(fhat)/length #Power spectrum
freq = np.fft.fftfreq(length)*2*np.pi/timestep
freq = fftfreq(length)*2*np.pi/timestep
L = np.arange(1, np.floor(length/2), dtype = 'int')

indices = PS > filter_level #Filter out the significant frequencies
PSClean = PS * indices #Zero out all other values
fhat = indices * fhat #Zero out small FT coeff.
fifft = np.fft.ifft(fhat) #Inverse FFT
return np.real(fifft)
fifft = ifft(fhat) #Inverse FFT
fifft = np.real(fifft)
return fifft

def damp(f, timestep, Tau):
"""
Dampen the signal in the time domain
Dampen a given signal in the time domain
Parameters
----------
f : np.ndarray
one-dimensional time-domain data
timestep : float/int
incremental change in the independent variable
Tau : float/int
Damping factor
Returns
-------
damped_sig : np.ndarray
dampened signal in the time domain using the
equation: f*e^(-t*Tau), where t is independent
axis.
"""
t = np.arange(0, len(f))*timestep
damped_sig = f*np.exp(-t/Tau)
return damped_sig

def FWHM(freq_f, timestep):
"""
Find the FWHM of the signal in the frequency domain
Find the Full Width Half Max of a function by returning the
width at the halfway point of the highest peak in the frequency
domain.
Parameters
----------
freq_f : np.ndarray
one-dimensional frequency-domain data
timestep : float/int
incremental change in the independent variable
Returns
-------
FWHM : float/int
the full width half max of the signal in the frequency domain
"""

length = len(freq_f)

PS = np.real(freq_f * np.conj(freq_f)/length)
freq = np.real(np.fft.fftfreq(length)*2*np.pi/timestep)
freq = np.real(fftfreq(length)*2*np.pi/timestep)
L = np.arange(1, np.floor(length/2), dtype = 'int')

peaks, _ = find_peaks(PS[L])
Expand All @@ -89,7 +138,8 @@ def FWHM(freq_f, timestep):
results_half = peak_widths(PS[L], peaks, rel_height=0.5)
FWHM = results_half[0][np.where(results_half[1] == max(results_half[1]))]*sf
max_height = max(results_half[1])*2
return FWHM[0]
FWHM = FWHM[0]
return FWHM

class Pade():
"""
Expand Down
3 changes: 2 additions & 1 deletion pycc/tests/test_012_FWHM.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Import required packages
from pycc.rt.utils import FWHM
from scipy.fftpack import fft
import numpy as np
import pytest

Expand All @@ -11,7 +12,7 @@ def test_FWHM():
f = np.cos(2*np.pi*12*t) + np.sin(2*np.pi*50*t)
f = f + np.random.randn(len(t))

fourier_transform = np.fft.fft(f, len(f))
fourier_transform = fft(f, len(f))
test_FWHM = FWHM(fourier_transform, timestep)

valid_FWHM = 6.30807298
Expand Down

0 comments on commit ac4c66c

Please sign in to comment.