Replaced for loop by vector calculation, limit memory size of smoothing matrix

This commit is contained in:
Casper Jansen 2023-01-17 09:05:39 +01:00
parent f5ed88cf07
commit b19c5ad38e
1 changed files with 8 additions and 8 deletions

View File

@ -102,7 +102,7 @@ def smoothCalcMatrix(freq, sw: SmoothingWidth):
# Initialize
L = len(freq)
Q = np.zeros(shape=(L, L))
Q = np.zeros(shape=(L, L), dtype=np.float16) # float16: keep size small
x0 = 1 if freq[0] == 0 else 0 # Skip first data point if zero frequency
# Loop over indices of raw frequency vector
@ -136,13 +136,11 @@ def smoothCalcMatrix(freq, sw: SmoothingWidth):
xl = xu-1 if xu-xl <= 0 else xl # Guarantee window length of at least 1
# Calculate window
gs = np.zeros(xu-xl)
for n, xi in enumerate(range(xl, xu)):
fi = freq[xi] # current frequency
gs[n] = np.sqrt( 1/ ((1+((fi/fc - fc/fi)*(1.507*Noct))**6)) )
gs /= np.sum(gs) # normalize: integral=1
Q[x, xl:xu] = gs # add to matrix
xg = np.arange(xl, xu) # indices
fg = freq[xg] # [Hz] corresponding freq
gs = np.sqrt( 1/ ((1+((fg/fc - fc/fg)*(1.507*Noct))**6)) ) # raw windw
gs /= np.sum(gs) # normalize: integral=1
Q[x, xl:xu] = gs # add to matrix
return Q
@ -209,6 +207,8 @@ def smoothSpectralData(freq, M, sw: SmoothingWidth,
# ..point is skipped. Not plotted any way.
# Find smoothing matrix. Look it up, otherwise calculate and store.
# TODO: find a way to reduce file size, loading time, a good place to store
# TODO: also save the last table in memory
fname = 'smoothing_tables.json' # storage file
nfft = int(2*(len(freq)-1))
key = f"nfft{nfft}_Noct{Noct}" # name