【问题标题】:Using lanczos low pass filter in R program在 R 程序中使用 lanczos 低通滤波器
【发布时间】:2018-12-24 19:14:23
【问题描述】:

我想知道是否有任何软件包可以让我们使用 Lanczos 过滤器。我找到了其他过滤器,例如 Butterworth,但我正在寻找 Lanczos 低通过滤器。

Lanczos 过滤器与巴特沃斯过滤器有何不同?任何建议或提示表示赞赏。

谢谢。

【问题讨论】:

标签: r lanczos


【解决方案1】:

我在网上找到了这个MATLAB implementation.

如果您跳过了第一部分(参数检查),编写它的 R 等效项看起来很简单。

#      Cf   - Cut-off frequency       (default: half Nyquist)
#      M    - Number of coefficients  (default: 100)
lanczos_filter_coef <- function(Cf,M=100){
  lowpass_cosine_filter_coef <- function(Cf,M)
    coef <- Cf*c(1,sin(pi*seq(M)*Cf)/(pi*seq(M)*Cf))
  hkcs <- lowpass_cosine_filter_coef(Cf,M)
  sigma <- c(1,sin(pi*seq(M)/M)/(pi*seq(M)/M))
  hkB <- hkcs*sigma
  hkA <- -hkB
  hkA[1] <- hkA[1]+1
  coef <- cbind(hkB, hkA)
  coef
}

以测试为例:

dT <- 1
Nf <- 1/(2*dT)
Cf <- Nf/2
Cf <- Cf/Nf
lanczos_filter_coef(Cf,5)

               hkB           hkA
[1,]  5.000000e-01  5.000000e-01
[2,]  2.977755e-01 -2.977755e-01
[3,]  1.475072e-17 -1.475072e-17
[4,] -5.353454e-02  5.353454e-02
[5,] -4.558222e-18  4.558222e-18
[6,]  2.481571e-18 -2.481571e-18

PS我不是很了解MATLAB(很多年前用过),所以我用this link作为R/MATLAB的类比。我希望有更多R/MATLAB/Scilab知识的人可以测试我的代码。

【讨论】:

  • 不错的发现!我建议 OP 对您的 R 代码与本机 Matlab 进行比较测试,看看结果是否一致。他要做的好作业:-)
【解决方案2】:

我使用了这个链接https://www.atmos.umd.edu/~ekalnay/syllabi/AOSC630/METO630ClassNotes13.pdf提供的方法,写了这个函数: `

lanczos_weights<-function(window=101,sampl_rate=1,type="lowpass",low_freq=1/100,high_freq=1/10){
low_freq=sampl_rate*low_freq
high_freq=sampl_rate*high_freq

if (type=="lowpass"){
    order = ((window - 1) %/% 2 ) + 1
    nwts = 2 * order + 1
    fc=low_freq
    w = seq(0,0,length=nwts)
    n = nwts %/% 2
    w[n+1] = 2 * fc
    k = seq(1, n-1)
    sigma = sin(pi * k / n) * n / (pi * k)
    firstfactor = sin(2 *pi * fc * k) / (pi * k)
    w[n:2] = firstfactor * sigma
    w[(n+2):(length(w)-1)] = firstfactor * sigma
    w=w[-c(1,length(w))]}
else if (type=="highpass"){
    order = ((window - 1) %/% 2 ) + 1
    nwts = 2 * order + 1
    fc=high_freq
    w = seq(0,0,length=nwts)
    n = nwts %/% 2
    w[n+1] = 2 * fc
    k = seq(1, n-1)
    sigma = sin(pi * k / n) * n / (pi * k)
    firstfactor = sin(2 *pi * fc * k) / (pi * k)
    w[n:2] = firstfactor * sigma
    w[(n+2):(length(w)-1)] = firstfactor * sigma
    w=w[-c(1,length(w))]
    w=-w
    w[order]=1-2*fc }
else if (type=="bandpass"){
    order = ((window - 1) %/% 2 ) + 1
    nwts = 2 * order + 1
    fc=low_freq
    w = seq(0,0,length=nwts)
    n = nwts %/% 2
    w[n+1] = 2 * fc
    k = seq(1, n-1)
    sigma = sin(pi * k / n) * n / (pi * k)
    firstfactor = sin(2 *pi * fc * k) / (pi * k)
    w[n:2] = firstfactor * sigma
    w[(n+2):(length(w)-1)] = firstfactor * sigma
    w1=w[-c(1,length(w))]

    order = ((window - 1) %/% 2 ) + 1
    nwts = 2 * order + 1
    fc=high_freq
    w = seq(0,0,length=nwts)
    n = nwts %/% 2
    w[n+1] = 2 * fc
    k = seq(1, n-1)
    sigma = sin(pi * k / n) * n / (pi * k)
    firstfactor = sin(2 *pi * fc * k) / (pi * k)
    w[n:2] = firstfactor * sigma
    w[(n+2):(length(w)-1)] = firstfactor * sigma
    w2=w[-c(1,length(w))]
    w=w2-w1}
else {print("Please specify a valid filter type: either 'lowpass', 'highpass' or 'bandpass'")}
return(w)}

`

#### the inputs are:
#### window: Filter length=number of weights. Corresponds to the total number of points to be lost. Should be odd: window=2N-1. The formula for N is taken from Poan et al. (2013)
#### sampl_rate: sampling rate=number of observation per time unit. ( eg: if time unit is one day, hourly data have sampl_rate=1/24)
#### type= one of "lowpass", "highpass" and "bandpass"
#### low_freq: the lowest frequency 
#### high_freq: the highest frequency

我将我的体重与使用 NCL filwgts_lanczos 获得的体重进行了比较,它们完全相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2016-02-29
    • 2011-05-29
    • 2019-04-29
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多