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

Brickwall filter : I still have a signal despite of setting bins to 0 #6

Open
system64MC opened this issue Jul 18, 2024 · 0 comments
Open

Comments

@system64MC
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant