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

View File

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