Final bit of cleanup in selcal-fft

This commit is contained in:
Jono Targett 2024-05-27 11:47:45 +09:30
parent e4b0645ac6
commit fa8332acc9
2 changed files with 20 additions and 22 deletions

View File

@ -5,29 +5,11 @@ from scipy.io import wavfile
from scipy.fft import fft from scipy.fft import fft
from filters import anti_alias from filters import anti_alias
from tones import TONES from tones import TONES
from utilities import *
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import sys import sys
file_name = sys.argv[1] file_name = sys.argv[1]
def decibels(f):
return 10 * np.log10(f)
def find_top_two_keys(d, threshold):
sorted_items = sorted(d.items(), key=lambda item: item[1], reverse=True)
if len(sorted_items) < 3:
return None
top_two = sorted_items[:2]
third_value = sorted_items[2][1]
if top_two[0][1] - third_value < threshold or top_two[1][1] - third_value < threshold:
return None
return top_two[0][0], top_two[1][0]
# Step 1: Read the WAV file
sample_rate, data = wavfile.read(file_name) sample_rate, data = wavfile.read(file_name)
# Handle stereo audio by converting to mono if needed # Handle stereo audio by converting to mono if needed
@ -92,7 +74,7 @@ for i in range(num_segments):
# Only import if we're actually plotting, these imports are pretty heavy. # Only import if we're actually plotting, these imports are pretty heavy.
import pyqtgraph as pg import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtWidgets, QtCore, mkQApp from pyqtgraph.Qt import QtWidgets, QtCore
app = pg.mkQApp("ImageView Example") app = pg.mkQApp("ImageView Example")
window = QtWidgets.QMainWindow() window = QtWidgets.QMainWindow()
@ -108,8 +90,7 @@ fft_view.setImage(fft_results)
fft_view.setRect(QtCore.QRectF(0, 0, len(data) // sample_rate, max_freq)) fft_view.setRect(QtCore.QRectF(0, 0, len(data) // sample_rate, max_freq))
plot.addItem(fft_view) plot.addItem(fft_view)
# Note JMT: Requires matplotlib installed to use this colormap colormap = pg.colormap.get("inferno")
colormap = pg.colormap.get("CMRmap", source='matplotlib')
colorbar = pg.ColorBarItem( values=(0,1), colorMap=colormap) colorbar = pg.ColorBarItem( values=(0,1), colorMap=colormap)
colorbar.setImageItem(fft_view, insert_in=plot) colorbar.setImageItem(fft_view, insert_in=plot)

17
scripts/utilities.py Normal file
View File

@ -0,0 +1,17 @@
import numpy as np
def decibels(f):
return 10 * np.log10(f)
def find_top_two_keys(d, threshold):
sorted_items = sorted(d.items(), key=lambda item: item[1], reverse=True)
if len(sorted_items) < 3:
return None
top_two = sorted_items[:2]
third_value = sorted_items[2][1]
if top_two[0][1] - third_value < threshold or top_two[1][1] - third_value < threshold:
return None
return top_two[0][0], top_two[1][0]