HihoCoder1388(Periodic Signal)-fft(快速傅里叶变换)

本文介绍了一种使用快速傅立叶变换(FFT)解决周期信号问题的方法。通过构造两个序列并计算它们的卷积,可以有效地找到问题的解。这种方法的时间复杂度为O(n*log(n)),能够高效地处理大规模数据。

题目链接:Periodic Signal


题意:求


思路:


求题目所给的最小值转化为了求的最大值。


构造序列两个


求这两个序列的卷积,就可以得到,k = 0, 1, 2, ..., n - 1,

求卷积用fft来实现,复杂度为o(n*log(n))


由于用fft算出来的数精度误差会比较大,可以用这个方法先算出k的位置,然后将k代入原始式子,求得答案。


代码:

# pragma comment(linker, "/STACK:1024000000,1024000000")
# include <iostream>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <cmath>
using namespace std;
typedef long long ll;
const long double PI = acos(-1.0);
const int maxn = 5e5 + 5;
int a[maxn], b[maxn];
int n;

struct Complex {
    long double x, y;
    Complex() : x(0.0), y(0.0) { }
    Complex(long double x, long double y) : x(x), y(y) { }
    Complex operator - (const Complex &b) const {
        return Complex(x - b.x, y - b.y);
    }
    Complex operator + (const Complex &b) const {
        return Complex(x + b.x, y + b.y);
    }
    Complex operator * (const Complex &b) const {
        return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
    }
} A[maxn], B[maxn];

void change(Complex y[], int len) {
    int i, j, k;
    for (i = 1, j = len / 2; i < len - 1; i++) {
        if (i < j) swap(y[i], y[j]);
        k = len / 2;
        while (j >= k) {
            j -= k; k /= 2;
        }
        if (j < k) j += k;
    }
}

void fft(Complex y[], int len, int on) {
    change(y, len);
    for (int h = 2; h <= len; h <<= 1) {
        Complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
        for (int j = 0; j < len; j += h) {
            Complex w(1, 0);
            for (int k = j; k < j + h / 2; k++) {
                Complex u = y[k];
                Complex t = w * y[k + h / 2];
                y[k] = u + t;
                y[k + h / 2] = u - t;
                w = w * wn;
            }
        }
    }
    if(on == -1)
    for (int i = 0; i < len; i++) y[i].x /= len;
}

ll sqr(ll x) {
    return x * x;
}

int main(void)
{
    int T; scanf("%d", &T);
    while (T-- && scanf("%d", &n)) {
        for (int i = 0; i < n; ++i) {
            scanf("%d", a + i);
            A[i] = Complex(a[i] * 1.0, 0.0);
        }
        for (int i = 0; i < n; ++i) {
            scanf("%d", b + i);
            B[n - i - 1] = Complex(b[i] * 1.0, 0.0);
        }
        for (int i = n; i < n * 2; ++i) B[i] = B[i - n], A[i] = Complex(0.0, 0.0);
        n *= 2;
        int len = 1;
        while (len < n) len <<= 1;
        len <<= 1;
        for (int i = n; i < len; ++i) A[i] = B[i] = Complex(0.0, 0.0);
        fft(A, len, 1); fft(B, len, 1);
        for (int i = 0; i < len; ++i) A[i] = A[i] * B[i];
        fft(A, len, -1);
        int k = 0;
        long double Max = A[n * 2 - 1].x;
        n /= 2;
        for (int i = n * 2 - 2; i >= n; --i) {
            if (Max < A[i].x) {
                Max = A[i].x; k = n * 2 - i - 1;
            }
        }
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            ans += sqr(a[i] - b[(i + k) % n]);
        }
        printf("%lld\n", ans);
    }

    return 0;
}


### MATLAB 中 FFT 频率不符实际解决方案 当遇到MATLAB中的快速傅里叶变换FFT)结果显示的频率与预期不一致的情况时,这通常是由几个常见原因引起的。了解这些因素有助于更精确地分析信号并获得正确的频率信息。 #### 1. 时间向量定义不当 时间间隔Δt的选择对于正确表示原始连续时间域内的离散样本至关重要。如果时间轴设置错误,则会影响整个频谱图的比例尺。应确保采样定理得到满足,并且时间序列长度N足够大以覆盖所需分辨率下的最低频率分量[^1]。 ```matlab Fs = 1000; % Sampling frequency (Hz) T = 1/Fs; % Sample time interval (seconds) L = 1000; % Length of signal t = (0:L-1)*T; % Time vector ``` #### 2. 正确理解归一化后的频率刻度 默认情况下,在执行`fft()`之后返回的是复数形式的数据点数组。为了可视化真实的物理意义下的幅度响应曲线,还需要进一步处理: - 计算双边频谱Pxx; - 将其转换成单边频谱P1; - 对于实数值输入信号来说,只保留前半部分即可; - 使用`abs()`获取绝对值作为振幅; - 应用适当的缩放因子来反映单位变化的影响; ```matlab Y = fft(x); P2 = abs(Y/L); % Two-sided spectrum P2. f = Fs*(0:(L/2))/L; plot(f,P1) % Single-sided amplitude spectrum. xlabel('Frequency (Hz)') ylabel('|P1(f)|') title('Single-Sided Amplitude Spectrum of x(t)'); grid on ``` #### 3. 考虑窗函数的应用 为了避免由于截断效应造成的泄漏现象——即能量扩散到相邻bin中去的现象,可以尝试应用不同的加权窗口技术如汉宁窗(Hann Window),这样可以在一定程度上改善估计精度[^2]。 ```matlab window = hann(L,'periodic'); % Periodic Hann window Xw = fft(window.*x); P2_w = abs(Xw/L); P1_w = P2_w(1:L/2+1); P1_w(2:end-1) = 2*P1_w(2:end-1); figure; subplot(2,1,1), plot(f,P1), title('Without Windowing'); subplot(2,1,2), plot(f,P1_w), title('With Hann Window Applied'); ``` 通过上述方法调整参数配置以及预处理手段,应该能够有效减少由软件实现细节所引发的误差问题,从而使得最终输出更加贴近理论预测结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值