Last active
August 29, 2015 14:07
-
-
Save dyadica/518bacfac54b890db6b1 to your computer and use it in GitHub Desktop.
A Unity MonoBehaviour implimentation of the Low Pass Filter as originally described by Thom Nichols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class SimpleLowPassFilter : MonoBehaviour | |
{ | |
// time smoothing constant for low-pass filter 0 ≤ alpha ≤ 1 | |
// a smaller value basically means more smoothing. | |
public float ALPHA = 0.15f; | |
// See: http://en.wikipedia.org/wiki/Low-pass_filter#Discrete-time_realization | |
// see http://en.wikipedia.org/wiki/Low-pass_filter#Algorithmic_implementation | |
// Store for current/smoothed values to apply on the next | |
// iteration | |
private float[] compassVals = new float[3]; | |
void Start () | |
{ | |
} | |
void Update() | |
{ | |
// Generate some dummy values | |
float[] currentVals = new float[3] { Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255) }; | |
// Apply the filter | |
compassVals = LowPassFilter(currentVals, compassVals); | |
print(compassVals[0].ToString() + "," + compassVals[1].ToString() + "," + compassVals[2].ToString()); | |
} | |
public float[] LowPassFilter(float[] input, float[] output) | |
{ | |
if (output == null) return input; | |
for (int i = 0; i < input.Length; i++) | |
{ | |
output[i] = output[i] + ALPHA * (input[i] - output[i]); | |
// output[i] = Mathf.Round(output[i]); | |
} | |
return output; | |
} | |
} | |
// <copyright file="SimpleLowPassFilter.cs" company="dyadica.co.uk"> | |
// Copyright (c) 2010, 2014 All Right Reserved, http://www.dyadica.co.uk | |
// This source is subject to the dyadica.co.uk Permissive License. | |
// Please see the http://www.dyadica.co.uk/permissive-license file for more information. | |
// All other rights reserved. | |
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY | |
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A | |
// PARTICULAR PURPOSE. | |
// </copyright> | |
// <author>SJB</author> | |
// <email>contact via facebook.com/adropinthedigitalocean</email> | |
// <date>29.10.2014</date> | |
// <summary>A Unity MonoBehaviour implimentation of the Low Pass Filter as originally described by Thom Nichols. | |
// Based on and using code from: http://blog.thomnichols.org/2011/08/smoothing-sensor-data-with-a-low-pass-filter. | |
// all kudos to Thom Nichols! | |
// </summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment