Skip to content

Instantly share code, notes, and snippets.

@Kailang
Created October 19, 2017 00:10
Show Gist options
  • Save Kailang/49e754dfd66b4d30c27020b8f5c75f24 to your computer and use it in GitHub Desktop.
Save Kailang/49e754dfd66b4d30c27020b8f5c75f24 to your computer and use it in GitHub Desktop.
Windowed Sinc Low Pass
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SincTrial : MonoBehaviour {
public int Phase, Period = 100;
public float Fc = 0.14f;
public int M = 100;
public float[] H, T;
public void Start() {
Visualizer.Rescale(0, 2048);
Visualizer.Rescale(1, 200);
}
public void OnAudioFilterRead(float[] data, int channels) {
H = new float[M + 1];
for (int i = 0; i <= M; i++) {
if (i - M / 2 == 0) H[i] = 2 * Mathf.PI * Fc;
else H[i] = Mathf.Sin(2 * Mathf.PI * Fc * (i - M / 2)) / (i - M / 2);
H[i] *= 0.42f + 0.8f * Mathf.Cos(4 * Mathf.PI * i / M) - 0.5f * Mathf.Cos(2 * Mathf.PI * i / M);
}
float sum = 0;
for (int i = 0; i <= M; i++) {
sum += H[i];
}
for (int i = 0; i <= M; i++) {
H[i] /= sum;
Visualizer.Push(1, H[i]);
}
var temp = new float[data.Length / channels];
for (int i = 0; i < temp.Length; i++) {
temp[i] = (Phase > (Period >> 1) ? 0.5f : 0);
Phase = (Phase + 1) % Period;
}
for (int i = 0; i < M; i++) {
data[i * channels] = temp[i];
}
for (int i = M; i < temp.Length; i++) {
for (int j = 0; j <= M; j++) {
data[i * channels] += temp[i - j] * H[j];
}
Visualizer.Push(0, data[i * channels]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment