-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from hybridmachine/FFT_Filters
Fft filters
- Loading branch information
Showing
21 changed files
with
776 additions
and
182 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace SignalProcessor.Filters | ||
{ | ||
public enum FilterType | ||
{ | ||
LOWPASS, | ||
HIGHPASS | ||
} | ||
|
||
public class WindowedSyncFilter : IWindowedSyncFilter | ||
{ | ||
/// <summary> | ||
/// Convenience parameter so users can name their filters for sorting, searching, etc | ||
/// </summary> | ||
public string Name { get; set; } | ||
public double CutoffFrequencySamplingFrequencyPercentage { get ; set ; } | ||
public int FilterLength { get ; set ; } | ||
public FilterType FilterType { get; set; } | ||
|
||
public WindowedSyncFilter() | ||
{ | ||
FilterType = FilterType.LOWPASS; // Lowpass by default | ||
} | ||
|
||
/// <summary> | ||
/// Implementation of the algorithm from P 290 Equation 16-4 of ISBN 0-9660176-3-3 "The Scientist and Engineer's Guide to Digital Signal Processing" | ||
/// https://www.dspguide.com/CH16.PDF#page=5&zoom=auto,-310,23 | ||
/// Note this filter kernel assumes samples are normalized by the caller | ||
/// </summary> | ||
/// <returns></returns> | ||
public virtual List<double> ImpulseResponse(bool normalize = true) | ||
{ | ||
List<double> impulseResponse; | ||
impulseResponse = new List<double>(FilterLength + 1); | ||
double normalizationFactor = 0.0; | ||
for (int idx = 0; idx <= FilterLength; idx++) | ||
{ | ||
if (idx == (FilterLength / 2)) | ||
{ | ||
impulseResponse.Add(2 * Math.PI * (CutoffFrequencySamplingFrequencyPercentage / 100.0)); | ||
} | ||
else | ||
{ | ||
double sincNumerator = (Math.Sin(2 * Math.PI * (CutoffFrequencySamplingFrequencyPercentage / 100.0) * (idx - (FilterLength / 2)))); | ||
double sincDenominator = (idx - (FilterLength / 2)); | ||
double blackmanWindow = (0.42 - (0.5 * Math.Cos((2 * Math.PI * idx)/FilterLength)) + (0.08 * Math.Cos((4 * Math.PI * idx)/FilterLength))); | ||
double value = (sincNumerator / sincDenominator) * blackmanWindow; | ||
impulseResponse.Add(value); | ||
} | ||
|
||
normalizationFactor += impulseResponse[impulseResponse.Count - 1]; | ||
} | ||
|
||
if (normalize) | ||
{ | ||
// Normalize for unity gain at DC | ||
for (int idx = 0; idx < impulseResponse.Count; idx++) | ||
{ | ||
impulseResponse[idx] = impulseResponse[idx] / normalizationFactor; | ||
} | ||
} | ||
|
||
// Spectral inversion | ||
if (FilterType == FilterType.HIGHPASS) | ||
{ | ||
// Spectral Inversion of the low pass filter | ||
for (int idx = 0; idx < impulseResponse.Count; idx++) | ||
{ | ||
impulseResponse[idx] = impulseResponse[idx] * -1; | ||
} | ||
|
||
impulseResponse[(impulseResponse.Count - 1) / 2] += 1; | ||
} | ||
|
||
return impulseResponse; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.Data.Sqlite; | ||
using SignalProcessor.Filters; | ||
using SignalsAndTransforms.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SignalsAndTransforms.DAL | ||
{ | ||
public static class FilterDAL | ||
{ | ||
public static bool Create(WorkBook workBook, Filter filter, SqliteConnection con) | ||
{ | ||
// For now we just clear and re-create signal entries, we may update values in place later | ||
// keep it simple for now | ||
string deleteSQL = $@"DELETE FROM Filters WHERE [Name]=@Name"; // This will cascade to the values table | ||
SqliteCommand cmd = con.CreateCommand(); | ||
cmd.CommandText = deleteSQL; | ||
cmd.Parameters.AddWithValue("@Name", filter.Name); | ||
cmd.ExecuteNonQuery(); | ||
|
||
string sql = $@"INSERT INTO Filters ([Name], [IsActive], [WorkBookId], [CutoffFrequencySamplingFrequencyPercentage], [FilterLength], [FilterType]) | ||
VALUES (@Name, @IsActive, @WorkBookId, @CutoffFrequencySamplingFrequencyPercentage, @FilterLength, @FilterType)"; | ||
|
||
|
||
|
||
cmd = con.CreateCommand(); | ||
cmd.CommandText = sql; | ||
cmd.Parameters.AddWithValue("@Name", filter.Name); | ||
cmd.Parameters.AddWithValue("@IsActive", filter.IsActive); | ||
cmd.Parameters.AddWithValue("@FilterType", filter.FilterType.ToString()); | ||
cmd.Parameters.AddWithValue("@FilterLength", filter.FilterLength); | ||
cmd.Parameters.AddWithValue("@CutoffFrequencySamplingFrequencyPercentage", filter.CutoffFrequencySamplingFrequencyPercentage); | ||
cmd.Parameters.AddWithValue("@WorkBookId", workBook.Id); | ||
cmd.ExecuteNonQuery(); | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.