R 卷积在数字信号处理中的应用

R 卷积在数字信号处理中的应用,r,vector,signal-processing,convolution,digital,R,Vector,Signal Processing,Convolution,Digital,我有一个简单的数字系统,输入x(n)=u(n)-u(n-4) 我试图通过'signal'包中的conv()函数或'stats'包中的convolve()函数找到输出y(n),并绘制y(n)与n的-10图≤ N≤ 十, 到目前为止,我有以下代码: library(signal) n <- c(-10:10) # Time index x <- c(rep(0, 10), rep(1, 4), rep(0, 7)) # Input S

我有一个简单的数字系统,输入x(n)=u(n)-u(n-4)

我试图通过'signal'包中的conv()函数或'stats'包中的convolve()函数找到输出y(n),并绘制y(n)与n的-10图≤ N≤ 十,

到目前为止,我有以下代码:

library(signal)

n <- c(-10:10)                           # Time index
x <- c(rep(0, 10), rep(1, 4), rep(0, 7)) # Input Signal
h1 <- c(rep(0, 11), 0.5, rep(0, 9))      # Filter 1
h2 <- 0.8^n                              # Filter 2
h2[0:11] <- 0                            #

system <- data.frame(n, x, h1, h2)


y <- conv(x + conv(x, h1), h2)           # Output Signal

system <- transform(system, y=y[1:21]) 

plot(system$n, system$y)  
库(信号)

我明白了。。conv()函数输出的中心与时间索引向量的中心对齐。因此:

library(signal)

n <- c(-10:10)                           # Time index
x <- c(rep(0, 10), rep(1, 4), rep(0, 7)) # Input Signal, square pulse
h1 <- c(rep(0, 11), 0.5, rep(0, 9))      # Filter 1
h2 <- 0.8^n                              # Filter 2
h2[1:10] <- 0                            #

system <- data.frame(n, x, h1, h2)

y <- conv(x + conv(x, h1)[11:31], h2)    # Output Signal

system <- transform(system, y=y[11:31]) 

plot(system$n, system$y)
库(信号)
N
library(signal) # Should this be inside the func. with attach(), detach()?

conv2 <- function(x, y){
    conv(x, y)[ceiling(length(x)/2):(length(x)+floor(length(x)/2))]
}

# so 
y <- conv2(x + conv2(x, h1), h2)
conv3 <- function(x, h){
m <- length(x)
n <- length(h)
X <- c(x, rep(floor(n/2), 0, floor(n/2)))   
H <- c(h, rep(floor(m/2), 0, floor(m/2)))
   Y <- vector()

for(i in 1:n+m-1){
    Y[i] <- 0 
    for(j in 1:m){
        Y[i] <- ifelse(i-j+1>0, Y[i] + X[j]*H[i-j+1], 0)
    }
}
Y[is.na(Y)] <- 0
Y[ceiling(m/2):(m+floor(m/2))]
}