You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
Hi! I wrote the following code :
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!
The text was updated successfully, but these errors were encountered: