-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps_f.py
73 lines (61 loc) · 2.6 KB
/
ps_f.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Function "repository" with general functions needed for calculation of power-spectrum.
Separated from run-script ps_run.py for convenience.
"""
import numpy as np
import math
import os
import time as tm
from detect_peaks import detect_peaks
import matplotlib.pyplot as plt
import create_pspectrum
def writer(fname, *args):
fname = os.path.join('Datafiles', fname + '.dat')
np.savetxt(fname, np.c_[args])
def reader(fname):
fname = os.path.join('Datafiles', fname + '.dat')
arr = np.loadtxt(fname)
return np.array_split(arr, len(arr[0, :]), axis=1)
def optimal_resolution(t, initial_resolution, sfreq, half_width=None, chunk_size=100, block=True):
"""
Finds a more optimal frequency resolution to reduce the amount of noise introduced
in the spectrum. Does it by simulating a sine function sampled similarly to the data set
and examines how the spectral response of it is at different power spectrum resolutions.
Assumes that the sine frequency used is not very significant for the result. As a sine
frequency, the frequency of maximum power could be used.
"""
if half_width is None:
half_width = sfreq - sfreq/100
yt = np.sin(2*math.pi*sfreq*t)
# Initial
pres = create_pspectrum.cuda(yt, t, freq_centre=[sfreq], half_width=half_width, resolution=initial_resolution,
chunk_size=chunk_size)[0]
# pres = create_pspectrum.nufftpy(yt, t, half_width=half_width, resolution=initial_resolution)
freq_i = pres[0]
power_i = pres[1]
# Find area under the curve (sum the power spectrum)
area_i = np.sum(power_i)
best_area = area_i
best_res = initial_resolution
best_freq = freq_i
best_p = power_i
# # # Loop to examine # # # #
resolution = np.linspace(0.90*initial_resolution, 2.5*initial_resolution, 10)
for current_res in resolution:
pres = create_pspectrum.numpy(yt, t, freq_centre=[sfreq], half_width=half_width, resolution=current_res,
chunk_size=chunk_size)[0]
# pres = create_pspectrum.nufftpy(yt, t, half_width=half_width, resolution=current_res)
current_power = pres[1]
current_area = np.sum(current_power)
if current_area < best_area:
best_area = current_area
best_res = current_res
best_freq = pres[0]
best_p = pres[1]
print('Best resolution: ', best_res)
print('Initial resolution: ', initial_resolution)
plt.figure()
plt.plot(freq_i/0.000001, power_i, '--')
plt.plot(best_freq/0.000001, best_p)
plt.show(block=block)
return best_res