Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format python code with black and imports with isort #151

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# format files with black
2eab92b73570d336cab356392bfcf1e08ef602e6

10 changes: 10 additions & 0 deletions .github/workflows/format_code.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Check code formatting

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: psf/black@stable
10 changes: 10 additions & 0 deletions .github/workflows/isort.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Run isort

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: isort/isort-action@v1
15 changes: 8 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from setuptools import Extension, setup
from Cython.Build import cythonize
import sys

import numpy
from Cython.Build import cythonize
from setuptools import Extension, setup

sourcefiles = [
"smstools/models/utilFunctions_C/utilFunctions.c",
"smstools/models/utilFunctions_C/cutilFunctions.pyx"
"smstools/models/utilFunctions_C/cutilFunctions.pyx",
]
if sys.platform == 'win32':
library = 'msvcrt'
if sys.platform == "win32":
library = "msvcrt"
else:
library = 'm'
library = "m"

extensions = [
Extension(
"smstools.models.utilFunctions_C.utilFunctions_C",
sourcefiles,
include_dirs=[numpy.get_include()],
libraries=[library]
libraries=[library],
),
]
setup(
Expand Down
54 changes: 34 additions & 20 deletions smstools/models/dftModel.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# functions that implement analysis and synthesis of sounds using the Discrete Fourier Transform
# (for example usage check dftModel_function.py in the interface directory)

import numpy as np
import math

import numpy as np
from scipy.fft import fft, ifft

from smstools.models import utilFunctions as UF

tol = 1e-14 # threshold used to compute phase


def dftModel(x, w, N):
"""
Analysis/synthesis of a signal using the discrete Fourier transform
x: input signal, w: analysis window, N: FFT size
returns y: output signal
"""
Analysis/synthesis of a signal using the discrete Fourier transform
x: input signal, w: analysis window, N: FFT size
returns y: output signal
"""

if not (UF.isPower2(N)): # raise error if N not a power of two
raise ValueError("FFT size (N) is not a power of 2")

if (w.size > N): # raise error if window size bigger than fft size
if w.size > N: # raise error if window size bigger than fft size
raise ValueError("Window size (M) is bigger than FFT size")

if all(x == 0): # if input array is zeros return empty output
Expand All @@ -35,13 +37,17 @@ def dftModel(x, w, N):
fftbuffer[-hM2:] = xw[:hM2]
X = fft(fftbuffer) # compute FFT
absX = abs(X[:hN]) # compute ansolute value of positive side
absX[absX < np.finfo(float).eps] = np.finfo(float).eps # if zeros add epsilon to handle log
absX[absX < np.finfo(float).eps] = np.finfo(
float
).eps # if zeros add epsilon to handle log
mX = 20 * np.log10(absX) # magnitude spectrum of positive frequencies in dB
pX = np.unwrap(np.angle(X[:hN])) # unwrapped phase spectrum of positive frequencies
# -----synthesis-----
Y = np.zeros(N, dtype=complex) # clean output spectrum
Y[:hN] = 10 ** (mX / 20) * np.exp(1j * pX) # generate positive frequencies
Y[hN:] = 10 ** (mX[-2:0:-1] / 20) * np.exp(-1j * pX[-2:0:-1]) # generate negative frequencies
Y[hN:] = 10 ** (mX[-2:0:-1] / 20) * np.exp(
-1j * pX[-2:0:-1]
) # generate negative frequencies
fftbuffer = np.real(ifft(Y)) # compute inverse FFT
y[:hM2] = fftbuffer[-hM2:] # undo zero-phase window
y[hM2:] = fftbuffer[:hM1]
Expand All @@ -50,10 +56,10 @@ def dftModel(x, w, N):

def dftAnal(x, w, N):
"""
Analysis of a signal using the discrete Fourier transform
x: input signal, w: analysis window, N: FFT size
returns mX, pX: magnitude and phase spectrum
"""
Analysis of a signal using the discrete Fourier transform
x: input signal, w: analysis window, N: FFT size
returns mX, pX: magnitude and phase spectrum
"""

if not (UF.isPower2(N)): # raise error if N not a power of two
raise ValueError("FFT size (N) is not a power of 2")
Expand All @@ -71,20 +77,26 @@ def dftAnal(x, w, N):
fftbuffer[-hM2:] = xw[:hM2]
X = fft(fftbuffer) # compute FFT
absX = abs(X[:hN]) # compute ansolute value of positive side
absX[absX < np.finfo(float).eps] = np.finfo(float).eps # if zeros add epsilon to handle log
absX[absX < np.finfo(float).eps] = np.finfo(
float
).eps # if zeros add epsilon to handle log
mX = 20 * np.log10(absX) # magnitude spectrum of positive frequencies in dB
X[:hN].real[np.abs(X[:hN].real) < tol] = 0.0 # for phase calculation set to 0 the small values
X[:hN].imag[np.abs(X[:hN].imag) < tol] = 0.0 # for phase calculation set to 0 the small values
X[:hN].real[
np.abs(X[:hN].real) < tol
] = 0.0 # for phase calculation set to 0 the small values
X[:hN].imag[
np.abs(X[:hN].imag) < tol
] = 0.0 # for phase calculation set to 0 the small values
pX = np.unwrap(np.angle(X[:hN])) # unwrapped phase spectrum of positive frequencies
return mX, pX


def dftSynth(mX, pX, M):
"""
Synthesis of a signal using the discrete Fourier transform
mX: magnitude spectrum, pX: phase spectrum, M: window size
returns y: output signal
"""
Synthesis of a signal using the discrete Fourier transform
mX: magnitude spectrum, pX: phase spectrum, M: window size
returns y: output signal
"""

hN = mX.size # size of positive spectrum, it includes sample 0
N = (hN - 1) * 2 # FFT size
Expand All @@ -96,7 +108,9 @@ def dftSynth(mX, pX, M):
y = np.zeros(M) # initialize output array
Y = np.zeros(N, dtype=complex) # clean output spectrum
Y[:hN] = 10 ** (mX / 20) * np.exp(1j * pX) # generate positive frequencies
Y[hN:] = 10 ** (mX[-2:0:-1] / 20) * np.exp(-1j * pX[-2:0:-1]) # generate negative frequencies
Y[hN:] = 10 ** (mX[-2:0:-1] / 20) * np.exp(
-1j * pX[-2:0:-1]
) # generate negative frequencies
fftbuffer = np.real(ifft(Y)) # compute inverse FFT
y[:hM2] = fftbuffer[-hM2:] # undo zero-phase window
y[hM2:] = fftbuffer[:hM1]
Expand Down
Loading
Loading