对于FFT幅度,dB标度不从零开始

时间:2018-04-12 15:14:52

标签: r fft

我正在尝试对加速计数据进行fft。但是,当我绘制幅度(以db为单位)与频率对比时,db scale不会从零开始。它从20开始直到-80。我想我按照他们的方式遵循所有步骤。

  1. 将信号零填充到下一个更高的2的幂(如果需要)
  2. 应用窗口功能
  3. 采用FFT
  4. 通过缩放将双面FFT输出转换为单面输出
  5. 应用窗口校正因子(相干增益)
  6. 查找信号幅度 - 绝对值和db
  7. 知道我可能做错了什么吗?我的整个代码如下所示。

    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中找到幅度是否正确?

0 个答案:

没有答案