📄 filter.cs
字号:
/*
* Filter functions, a part of DSProcessing library.
* author: Jan Sova
* mailto: twardowski@email.cz
*
* -------------------------------------------------------------------------
*
* DSProcessing - C#/C++ library of signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 2007-2008
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace DSProcessing
{
class Filter
{
/// <summary>
/// Fuction known from Matlab.
/// </summary>
/// <param name="b"></param>
/// <param name="a"></param>
/// <param name="data"></param>
/// <returns></returns>
public static double[] filtfilt(double[] b, double[] a, double[] data)
{
//pripravna cast
int nfilt = (int)Tools.max(b.Length, a.Length);
int nfact = nfilt * 3 - 1;
int len = data.Length;
if (len <= nfact) // input data too short!
{
Console.WriteLine("input data too short!");
}
double[] d = new double[data.Length + 2 * nfilt];
//interpolation
for (int i = 0; i < nfilt; i++)
d[i] = 2 * data[0] - data[nfilt + 1 - i];
for (int i = nfilt; i < nfilt + data.Length; i++)
d[i] = data[i - nfilt];
int k = 0;
for (int i = nfilt + data.Length; i < nfilt + data.Length; i++)
{
d[i] = 2 * data[data.Length] - data[nfilt - 2 - k];
k++;
}
//2*x(1)-x((nfact+1):-1:2);x;2*x(len)-x((len-1):-1:len-nfact)];
//filtration
double[] x = filt(b, a, data);
x = Tools.timereverse(x);
double[] z = filt(b, a, x);
z = Tools.timereverse(z);
return z;
}
/// <summary>
/// CZ: Jestli se nepletu, tak pole a[] a b[] museji byt stejne dlouha! Je to opravnene?
/// EN: Filtration with filter given by a[]/b[].
/// </summary>
/// <param name="b"></param>
/// <param name="a"></param>
/// <param name="data"></param>
/// <returns>a[] and b[] must be the same length</returns>
public static double[] filt(double[] b, double[] a, double[] data)
{
double[] y = new double[data.Length];
for (int i = 0; i < data.Length; i++)
y[i] = 0;
for (int n = 0; n < b.Length; n++)
{
int max = n;
//CZ: zpetnovazebni cast
for (int k = 0; k <= max; k++)
y[n] += b[k] * data[n - k];
for (int k = 1; k <= max; k++)
y[n] -= a[k] * y[n - k];
}
for (int n = b.Length; n < data.Length; n++)
{
//dopredna cast
for (int k = 1; k < a.Length; k++)
y[n] -= a[k] * y[n - k];
//zpetnovazebni cast
for (int k = 0; k < b.Length; k++)
y[n] += b[k] * data[n - k];
}
return y;
}
/// <summary>
/// CZ: klouzavy prumer
/// </summary>
/// <param name="data"></param>
/// <param name="M"></param>
/// <returns></returns>
public static double[] MAfilter(double[] data, int M)
{
double[] returnValue = new double[data.Length];
//
for (int n = M; n < data.Length; n++)
{
for (int m = 0; m <= M; m++)
{
returnValue[n] += data[n - M];
}
returnValue[n] /= (M + 1);
}
//CZ: jeste doresit prvni vzorky
for (int n = 0; n < M; n++)
{
int max = n;
for (int m = 0; m <= max; m++)
{
returnValue[n] += data[n - m];
}
returnValue[n] /= (max + 1);
}
return returnValue;
}
///test sequence for N = 3
///in: 10, 12, 11, 0, 9, 8, 10, 15, 14, 100, 13, 20, 15
///out: 10, 11, 11, 9, 8, 9, 10, 14, 15, 14, 20, 15, 15
///test sequence for N = 5
///in: 10, 12, 11, 0, 9, 8, 10, 15, 14, 100, 13, 20, 15
///out: 10, 10, 10, 9, 9, 9, 10, 14, 14, 15, 15, 15, 13
/// <summary>
/// Median filtration.
/// </summary>
/// <param name="data"></param>
/// <param name="N">rank</param>
/// <returns></returns>
public static double[] medfilt1(double[] data, int N)
{
double[] returnValue = new double[data.Length];
double[] data1 = new double[data.Length + (N / 2) * 2];
double[] okno = new double[N];
int NPul = N / 2;
for (int i = 0; i < N / 2; i++)
data1[i] = 0;
for (int i = 0; i < data.Length; i++)
data1[NPul + i] = data[i];
for (int i = data.Length + NPul; i < data.Length + 2 * NPul; i++)
data1[i] = 0;
//CZ: pro sudy rad
if ((N / 2) * 2 == N)
{
for (int i = 0; i < data1.Length - N; i++)
{
for (int j = 0; j < N; j++)
{
okno[j] = data1[i + j];
}
returnValue[i] = Tools.mean(okno);
}
}
//CZ: pro lichy rad
else
{
for (int i = 0; i < data1.Length - N + 1; i++)
{
for (int j = 0; j < N; j++)
{
okno[j] = data1[i + j];
}
//<todo>CZ: Implementovat lepsi tridici algoritmus</todo>
Tools.insertSort(okno);
returnValue[i] = okno[N / 2];
}
}
return returnValue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -