Skip to content

Commit

Permalink
Cleanup docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
smu160 committed Apr 10, 2024
1 parent 31e9a55 commit 1a1a51c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
7 changes: 4 additions & 3 deletions pyphastft/example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy.fft import fft

from pyphastft import fft
# from pyphastft import fft


def main():
Expand All @@ -16,7 +17,7 @@ def main():
) # Adjusted time vector

# Generate the signal
s_re = 2 * np.sin(2 * np.pi * t) + np.sin(2 * np.pi * 10 * t)
s_re = 2 * np.sin(2 * np.pi * t + 1) + np.sin(2 * np.pi * 10 * t + 1)
s_im = np.ascontiguousarray([0.0] * len(s_re), dtype=np.float64)

# Plot the original signal
Expand All @@ -30,7 +31,7 @@ def main():
plt.legend()

# Perform FFT
fft(s_re, s_im, direction="f")
s_re = fft(s_re)

# Plot the magnitude spectrum of the FFT result
plt.subplot(2, 1, 2)
Expand Down
4 changes: 2 additions & 2 deletions pyphastft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use numpy::PyReadwriteArray1;
use phastft::{fft as fft_rs, planner::Direction};
use phastft::{fft_64 as fft_64_rs, planner::Direction};
use pyo3::prelude::*;

#[pyfunction]
Expand All @@ -11,7 +11,7 @@ fn fft(mut reals: PyReadwriteArray1<f64>, mut imags: PyReadwriteArray1<f64>, dir
Direction::Reverse
};

fft_rs(
fft_64_rs(
reals.as_slice_mut().unwrap(),
imags.as_slice_mut().unwrap(),
dir,
Expand Down
20 changes: 10 additions & 10 deletions pyphastft/vis_qt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pyphastft import fft
import sys

import numpy as np
import pyaudio
import pyqtgraph as pg
from pyphastft import fft
from pyqtgraph.Qt import QtWidgets, QtCore
import sys


class RealTimeAudioSpectrum(QtWidgets.QWidget):
Expand All @@ -28,7 +29,7 @@ def init_ui(self):
self.plot_widget.setBackground("k")
self.plot_item = self.plot_widget.getPlotItem()
self.plot_item.setTitle(
"Real-Time Audio Spectrum Visualizer\npowered by PhastFT",
"Real-Time Audio Spectrum Visualizer powered by PhastFT",
color="w",
size="16pt",
)
Expand Down Expand Up @@ -79,10 +80,10 @@ def init_audio_stream(self):
def audio_callback(self, in_data, frame_count, time_info, status):
audio_data = np.frombuffer(in_data, dtype=np.float32)
reals = np.zeros(self.n_fft_bins)
imaginaries = np.zeros(self.n_fft_bins)
imags = np.zeros(self.n_fft_bins)
reals[: len(audio_data)] = audio_data # Fill the reals array with audio data
fft(reals, imaginaries, direction="f")
fft_magnitude = np.sqrt(reals**2 + imaginaries**2)[: self.n_fft_bins // 2]
fft(reals, imags, direction="f")
fft_magnitude = np.sqrt(reals**2 + imags**2)[: self.n_fft_bins // 2]

# Aggregate or interpolate FFT data to fit into display bins
new_fft_data = np.interp(
Expand All @@ -93,13 +94,12 @@ def audio_callback(self, in_data, frame_count, time_info, status):

# Apply exponential moving average filter
self.ema_fft_data = self.ema_fft_data * self.smoothing_factor + new_fft_data * (
1 - self.smoothing_factor
1.0 - self.smoothing_factor
)
return (in_data, pyaudio.paContinue)
return in_data, pyaudio.paContinue

def update(self):
if hasattr(self, "ema_fft_data"):
self.bar_graph.setOpts(height=self.ema_fft_data, width=self.bar_width)
self.bar_graph.setOpts(height=self.ema_fft_data, width=self.bar_width)

def closeEvent(self, event):
self.stream.stop_stream()
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ macro_rules! impl_fft_for {
///
/// # Panics
///
/// Panics if `reals.len() != imags.len()` or if `reals.len()` and `imags.len()` are not a power of
/// 2
/// Panics if `reals.len() != imags.len()`, or if the input length is _not_ a power of 2.
///
/// ## References
/// <https://inst.eecs.berkeley.edu/~ee123/sp15/Notes/Lecture08_FFT_and_SpectAnalysis.key.pdf>
Expand Down Expand Up @@ -77,8 +76,7 @@ macro_rules! impl_fft_with_opts_and_plan_for {
///
/// # Panics
///
/// Panics if `reals.len() != imags.len()` or if `reals.len()` and `imags.len()` are not a power of
/// 2
/// Panics if `reals.len() != imags.len()`, or if the input length is _not_ a power of 2.
pub fn $func_name(
reals: &mut [$precision],
imags: &mut [$precision],
Expand Down

0 comments on commit 1a1a51c

Please sign in to comment.