///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Edouard Griffiths, F4EXB <f4exb06@gmail.com>               //
//                                                                               //
// This program is free software; you can redistribute it and/or modify          //
// it under the terms of the GNU General Public License as published by          //
// the Free Software Foundation as version 3 of the License, or                  //
// (at your option) any later version.                                           //
//                                                                               //
// This program is distributed in the hope that it will be useful,               //
// but WITHOUT ANY WARRANTY; without even the implied warranty of                //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
// GNU General Public License V3 for more details.                               //
//                                                                               //
// You should have received a copy of the GNU General Public License             //
// along with this program. If not, see <http://www.gnu.org/licenses/>.          //
///////////////////////////////////////////////////////////////////////////////////

#include "mainbench.h"
#include "dsp/fftfilterrrc.h"
#include <QDebug>

void MainBench::testFFTRRCFilter()
{
    qDebug() << "MainBench::testFFTRRCFilter";
    const int RRC_FFT_SIZE = 512;
    std::complex<float>* rrcFilterOut = nullptr;
    FFTFilterRRC filter(RRC_FFT_SIZE);
    filter.create(0.05f, 0.35f);  // 2400 baud, 0.35 rolloff

    qDebug() << "MainBench::testFFTRRCFilter: filter created";
    FILE *fd_filter = fopen("test_rrc_filter.txt", "w");
    if (!fd_filter)
    {
        qWarning() << "MainBench::testFFTRRCFilter: failed to open test_rrc_filter.txt : "
                   << strerror(errno);
        return;
    }

    for (int i = 0; i < RRC_FFT_SIZE; i++) {
        fprintf(fd_filter, "%f\n", static_cast<double>(std::abs(filter.getFilter()[i])));
    }
    qDebug() << "MainBench::testFFTRRCFilter: filter coefficients written to test_rrc_filter.txt";
    fclose(fd_filter);

    qDebug() << "MainBench::testFFTRRCFilter: running filter";
    FILE *fd = fopen("test_rrc.txt", "w");
    if (!fd)
    {
        qWarning() << "MainBench::testFFTRRCFilter: failed to open test_rrc.txt : "
                   << strerror(errno);
        return;
    }

    int outLen = 0;

    for (int i = 0; i < 5000; i++)
    {
        int ss = 48000 / 2400;  // Samples per symbol at 2400 baud
        int d = i / ss;  // Symbol index at 2400 baud
        Real s = (d % 2 == 0) ? 1.0f : -1.0f;  // BPSK symbol sequence
        Real x = (i % ss == 0 ? s : 0.0f);  // Pulsed signal at 2400 Hz
        // Real phi = i * (1200.0 / 48000.0) * (2*3.141);
        // Real x = sin(phi) > 0.0 ? 1.0f : -1.0f;  // Simulate a BPSK signal at 1200 baud
        filter.process(Complex(x, 0.0f), &rrcFilterOut);
        outLen++;

        if (outLen % (RRC_FFT_SIZE / 2) == 0)
        {
            // printf("\ni=%d n_out: %d\n", i, n_out);

            for (int j = 0; j < outLen; j++)
            {
                Complex rrc = rrcFilterOut[j];
                fprintf(fd, "%f\n", rrc.real());
            }

            outLen = 0;
        }
    }

    // output any remaining samples
    for (int j = 0; j < outLen; j++)
    {
        Complex rrc = rrcFilterOut[j];
        fprintf(fd, "%f\n", rrc.real());
    }

    qDebug() << "MainBench::testFFTRRCFilter: output samples written to test_fftrrc.txt";
    fclose(fd);
}
