-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfft.py
51 lines (43 loc) · 1.68 KB
/
fft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
import numpy as np
import scipy as scp
import scipy.signal as signal
from collections.abc import Iterable
def fft(record,t_or_dt):
"""
% (C) Nick Holschuh - Amherst College -- 2024 ([email protected])
% This function calculates the fft and the frequency axis for an input record
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are as follows:
% record -- the timeseries to take the fft of
% t_or_dt -- either the delta time for the record, or the full set of times
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The outputs are as follows:
% f -- the frequency axis for use in plotting
% spectrum -- the frequency spectrum
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
"""
if isinstance(t_or_dt,Iterable) == 0:
dt = t_or_dt
else:
if len(t_or_dt) > 1:
dt = np.nanmedian(np.diff(t_or_dt))
else:
dt = t_or_dt
apply_window = 0
if apply_window == 1:
max_window_size = 100
if len(record) < max_window_size:
window = np.hanning(len(record))
else:
window = np.ones(record.shape)
mid_index = int(max_window_size/2+1)
hw = np.hanning(max_window_size)
window[0:mid_index] = hw[:mid_index]
window[-mid_index:] = hw[-mid_index:]
record = record * window
spectrum = scp.fft.fftshift(scp.fft.fft(record))
f = scp.fft.fftshift(scp.fft.fftfreq(len(record),dt))
return {'f':f,'spectrum':spectrum}