arnetheduck / nim-fftr Goto Github PK
View Code? Open in Web Editor NEWThe fastest Fourier transform in the Rhein (so far). Pure Nim.
License: MIT License
The fastest Fourier transform in the Rhein (so far). Pure Nim.
License: MIT License
Testing out your code with a SciPy example and I am getting the incorrect inverse. Am I doing something wrong?
SciPy:
>>> from scipy.fft import fft, ifft
>>> import numpy as np
>>> x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
>>> y = fft(x)
>>> y
array([ 4.5 +0.j , 2.08155948-1.65109876j,
-1.83155948+1.60822041j, -1.83155948-1.60822041j,
2.08155948+1.65109876j])
>>> yinv = ifft(y)
>>> yinv
array([ 1.0+0.j, 2.0+0.j, 1.0+0.j, -1.0+0.j, 1.5+0.j])
My example:
let input = @[complex64(1.0, 0.0), complex64(2.0, 0.0), complex64(1.0, 0.0), complex64(-1.0, 0.0), complex64(1.5, 0.0)]
let output = fft(input, false)
echo output
let iOutput = fft(output, true)
echo iOutput
Results in:
@[(4.5, -3.608224830031759e-16), (2.081559480312316, -1.651098762732523), (-1.831559480312317, 1.60822040644407), (-1.831559480312316, -1.60822040644407), (2.081559480312317, 1.651098762732523)]
@[(5.0, -8.881784197001252e-16), (10.0, -1.77635683940025e-15), (4.999999999999999, -4.440892098500626e-16), (-4.999999999999999, 8.881784197001252e-16), (7.5, 0.0)]
fftr is an very handy library. I am porting some code from numpy to nim and utilize the fftr libaray.
Forward FFT works well, but IFFT seems not match with numpy.fft.ifft.
After divider fftr result with FFT points, they are match again.
fft(some_complex_seq ,true)/fft_num_of_points => this match with numpy.fft.ifft
Code
import fftr, std/math, std/sequtils
let
signal = (0..1023).mapIt(complex64(sin(TAU * 0.1 * float64(it))))
freqs = fft(tmp, false) # False for forward, true for inverse FFT
Error message
Hint: used config file '/home/anon/.choosenim/toolchains/nim-2.0.2/config/nim.cfg' [Conf]
Hint: used config file '/home/anon/.choosenim/toolchains/nim-2.0.2/config/config.nims' [Conf]
......................................................................
/home/anon/Documents/ProgrammingLearning/nim/FFT/fftr.nim(1, 8) Error: module 'fftr' cannot import itself
System Info
--------
OS: Arch Linux x86_64
Host: Oryx Pro (oryp8)
Kernel: 6.6.10-arch1-1
Shell: bash 5.2.21
DE: KDE Plasma 5.27.10
WM: KWin (Wayland)
WM Theme: Breeze
CPU: 11th Gen Intel(R) Core(TM) i7-11800H (16) @ 4.60 GHz
GPU 1: Intel UHD Graphics
GPU 2: NVIDIA GeForce RTX 3070 Mobile / Max-Q
Memory: 31.20 GiB
I am not entirely sure why I am getting the import error message. I installed the package using Nimble.
Hi! I wrote the following code :
import fftr, std/[math, sequtils]
import strutils
let
# Square wave
# signal = (0..1023).mapIt(complex64(if(it < 512): 1.0 else: -1.0))
# Saw wave
# signal = (0..1023).mapIt(complex64(2.0 * ((float(it * 12) / 1023.0) mod 1.0) - 1))
# Sine wave
signal = (0..1023).mapIt(complex64(sin(12 * 2 * PI * it.toFloat()/1024.0)))
# getting freqs
var frequencies = fft(signal, false).mapIt(it / 1024.0)
echo frequencies.len
# We filter out every harmonic above 10...
const cutoff = 10
for i in countdown(1023 - cutoff, cutoff + 1):
frequencies[i] = frequencies[i] * complex(0.0, 0.0)
var signalComplex = fft((0..1023).mapIt(frequencies[it]), true)
var signalAgain = signalComplex.mapIt(it.re)
assert(signalAgain.len <= 1024)
echo "Output signal : ", signalAgain
var outS16: array[1024, int16]
for i in 0..<1024:
outS16[i] = (signalAgain[i] * 20000000000000000000.0).int16
import streams
var f = newFileStream("outWave.bin", fmWrite)
if not f.isNil:
f.write outS16
f.flush
I filter out every harmonic that is above the 10th. But when I filter a signal (here, a pure sine wave) with higher harmonics, I still have a signal, very silent, but I still get something instead of 0.
This is very problematic in case I want to normalize the signal.
Is that a bug or due to floating points doing suspicious things?
Thanks for your answer!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.