fft over a rolling sample window for greater bin precision
This commit is contained in:
parent
cf8a9dcfc1
commit
8aecf684f9
@ -95,34 +95,41 @@ delta_f = sample_rate / segment_size
|
||||
# Determine the bin range for desired frequency range (100 Hz to 2000 Hz)
|
||||
high_bin = int(max_freq / delta_f)
|
||||
|
||||
seg_off = int(sample_rate * 0.1)
|
||||
act_segs = len(filtered_data) // seg_off
|
||||
|
||||
# Initialize a 2D array to store DFT results (magnitude spectrum)
|
||||
# Only store the bins within the desired frequency range
|
||||
dft_results = np.zeros((num_segments, high_bin))
|
||||
dft_results = np.zeros((act_segs, high_bin))
|
||||
|
||||
for i in range(num_segments):
|
||||
start = i * segment_size
|
||||
end = start + segment_size
|
||||
segment = filtered_data[start:end]
|
||||
for i in range(act_segs):
|
||||
end = (i+1) * seg_off
|
||||
start = end - segment_size
|
||||
|
||||
# Step 4: Apply the DFT
|
||||
dft_result = fft(segment)
|
||||
try:
|
||||
segment = filtered_data[start:end]
|
||||
|
||||
magnitudes = np.abs(dft_result)
|
||||
total_energy = np.sum(magnitudes ** 2)
|
||||
normalized_magnitudes = magnitudes / np.sqrt(total_energy)
|
||||
normalized_magnitude = np.mean(normalized_magnitudes)
|
||||
# Step 4: Apply the DFT
|
||||
dft_result = fft(segment)
|
||||
|
||||
# Store the magnitude spectrum in the 2D array, only for the desired frequency range
|
||||
dft_results[i, :] = normalized_magnitudes[:high_bin]
|
||||
magnitudes = np.abs(dft_result)
|
||||
total_energy = np.sum(magnitudes ** 2)
|
||||
normalized_magnitudes = magnitudes / np.sqrt(total_energy)
|
||||
normalized_magnitude = np.mean(normalized_magnitudes)
|
||||
|
||||
scores = [
|
||||
10 * np.log10(np.sum(normalized_magnitudes[int((f-w)/delta_f):int((f+w)/delta_f)]))
|
||||
for f,w in zip(frequencies, widths)
|
||||
]
|
||||
# Store the magnitude spectrum in the 2D array, only for the desired frequency range
|
||||
dft_results[i, :] = normalized_magnitudes[:high_bin]
|
||||
|
||||
codes = get_largest_two_indices(scores, 3.0)
|
||||
if codes:
|
||||
print([frequencies[code] for code in codes])
|
||||
scores = [
|
||||
10 * np.log10(np.sum(normalized_magnitudes[int((f-w)/delta_f):int((f+w)/delta_f)]))
|
||||
for f,w in zip(frequencies, widths)
|
||||
]
|
||||
|
||||
codes = get_largest_two_indices(scores, 3.0)
|
||||
if codes:
|
||||
print([frequencies[code] for code in sorted(codes)])
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# Step 5: Plot the spectrogram
|
||||
|
||||
Loading…
Reference in New Issue
Block a user