我正在尝试对加速计数据进行fft。但是,当我绘制幅度(以db为单位)与频率对比时,db scale不会从零开始。它从20开始直到-80。我想我按照他们的方式遵循所有步骤。
知道我可能做错了什么吗?我的整个代码如下所示。
library(signal)
library(pracma)
signal_in <- pz$ACC[1:1000]
fs <- 5000
zero_padding <- FALSE
windowfn <- "rectangle"
demean <- FALSE
detrend <- FALSE
# Length of the signal
L <- length(signal_in)
# Removing the trend or mean as required
if (detrend == TRUE) {
signal_in <- detrend(signal_in, "linear")
} else if (demean == TRUE) {
signal_in <- detrend(signal_in, "constant")
}
# Various window functions
if (windowfn == "hanning") {
window_coeff <- hanning(L)
amp_correction_factor <- mean(window_coeff)
} else if (windowfn == "hamming") {
window_coeff <- hamming(L)
amp_correction_factor <- mean(window_coeff)
} else if (windowfn == "flattop") {
window_coeff <- flattopwin(L, "symmetric")
amp_correction_factor <- mean(window_coeff)
} else if (windowfn == "rectangle") {
window_coeff <- rep(1, L)
amp_correction_factor <- mean(window_coeff)
}
# Apply the window function and if required, zero padding
# L is the length of the original signal
# N is the length of the transformed signal or the number of points in the FFT (to the next higher power of 2)
if (zero_padding == TRUE) {
N <- 2^nextpow2(L)
if (L < N) {
zero_pad <- rep(0, (N - L))
signal_in_w <- signal_in * window_coeff
signal_in_w <- c(signal_in_w, zero_pad)
} else if (L == N) {
signal_in_w <- signal_in * window_coeff
N <- L
}
} else {
if (L %% 2 != 0) {
signal_in_w <- signal_in * window_coeff
signal_in_w <- c(signal_in_w, 0)
N <- L + 1
} else {
signal_in_w <- signal_in * window_coeff
N <- L
}
}
# Double-sided FFT
signal.fft <- fft(signal_in_w)
# Single-sided FFT with normalisation - signal amplitude (absolute)
signal.amp.abs <- (2/L) * Mod(signal.fft[1:(length(signal.fft)/2)])
# the DC component should not be doubled so halving it
signal.amp.abs[1] <- signal.amp.abs[1] / 2
# window scaling (coherent gain)
signal.amp.abs <- signal.amp.abs / amp_correction_factor
# signal amplitude (dB)
signal.amp.db <- 20*log10(signal.amp.abs)
# Frequency vector
signal.freq <- seq(from = 0, to = fs/2, length.out = length(signal.fft)/2)
# Plot signal amplitude (absolute) vs frequency (Hz)
plot(signal.amp.abs ~ signal.freq, t = "l")
# Plot signal amplitude (dB) vs frequency (Hz)
plot(signal.amp.db ~ signal.freq, t = "l")
我的计算在db中找到幅度是否正确?