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)
|
# Determine the bin range for desired frequency range (100 Hz to 2000 Hz)
|
||||||
high_bin = int(max_freq / delta_f)
|
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)
|
# Initialize a 2D array to store DFT results (magnitude spectrum)
|
||||||
# Only store the bins within the desired frequency range
|
# 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):
|
for i in range(act_segs):
|
||||||
start = i * segment_size
|
end = (i+1) * seg_off
|
||||||
end = start + segment_size
|
start = end - segment_size
|
||||||
segment = filtered_data[start:end]
|
|
||||||
|
|
||||||
# Step 4: Apply the DFT
|
try:
|
||||||
dft_result = fft(segment)
|
segment = filtered_data[start:end]
|
||||||
|
|
||||||
magnitudes = np.abs(dft_result)
|
# Step 4: Apply the DFT
|
||||||
total_energy = np.sum(magnitudes ** 2)
|
dft_result = fft(segment)
|
||||||
normalized_magnitudes = magnitudes / np.sqrt(total_energy)
|
|
||||||
normalized_magnitude = np.mean(normalized_magnitudes)
|
|
||||||
|
|
||||||
# Store the magnitude spectrum in the 2D array, only for the desired frequency range
|
magnitudes = np.abs(dft_result)
|
||||||
dft_results[i, :] = normalized_magnitudes[:high_bin]
|
total_energy = np.sum(magnitudes ** 2)
|
||||||
|
normalized_magnitudes = magnitudes / np.sqrt(total_energy)
|
||||||
|
normalized_magnitude = np.mean(normalized_magnitudes)
|
||||||
|
|
||||||
scores = [
|
# Store the magnitude spectrum in the 2D array, only for the desired frequency range
|
||||||
10 * np.log10(np.sum(normalized_magnitudes[int((f-w)/delta_f):int((f+w)/delta_f)]))
|
dft_results[i, :] = normalized_magnitudes[:high_bin]
|
||||||
for f,w in zip(frequencies, widths)
|
|
||||||
]
|
|
||||||
|
|
||||||
codes = get_largest_two_indices(scores, 3.0)
|
scores = [
|
||||||
if codes:
|
10 * np.log10(np.sum(normalized_magnitudes[int((f-w)/delta_f):int((f+w)/delta_f)]))
|
||||||
print([frequencies[code] for code in codes])
|
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
|
# Step 5: Plot the spectrogram
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user