Files
sdrangel/sdrbench/test_firrrc.cpp
T
Robin Getz 563b73f15f sdrbench: fix RRC and FFT test file handling
Address cppcheck warnings by checking fopen() results before using the
returned file handles and reporting the underlying error on failure.

This prevents null file handles from being passed to fprintf() and
fclose(), avoiding a potential crash when test output files cannot be
created.

Explicitly convert RRC filter coefficients to double for fprintf() to
match the %f format specifier.

Signed-off-by: Robin Getz <rgetz503@gmail.com>
2026-08-10 20:48:15 -04:00

63 lines
2.8 KiB
C++

///////////////////////////////////////////////////////////////////////////////////
// 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/firfilterrrc.h"
#include <QDebug>
void MainBench::testFIRRRCFilter()
{
qDebug() << "MainBench::testFIRRRCFilter";
FIRFilterRRC filter;
filter.create(0.05f, 0.35f, 8, FIRFilterRRC::Normalization::Gain); // 2400 baud, 0.35 rolloff
qDebug() << "MainBench::testFIRRRCFilter: filter created";
FILE *fd_filter = fopen("test_rrc_filter.txt", "w");
if (!fd_filter)
{
qWarning() << "MainBench::testFIRRRCFilter: failed to open test_rrc_filter.txt : "
<< strerror(errno);
return;
}
const std::vector<float>& taps = filter.getTaps();
for (auto tap : taps) {
fprintf(fd_filter, "%f\n", static_cast<double>(std::abs(tap)));
}
qDebug() << "MainBench::testFIRRRCFilter: filter coefficients written to test_rrc_filter.txt";
fclose(fd_filter);
qDebug() << "MainBench::testFIRRRCFilter: running filter";
FILE *fd = fopen("test_rrc.txt", "w");
if (!fd)
{
qWarning() << "MainBench::testFIRRRCFilter: failed to open test_rrc.txt : "
<< strerror(errno);
return;
}
for (int i = 0; i < 1000; i++)
{
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
Complex rrc = filter.filter(Complex(x, 0.0f));
fprintf(fd, "%f\n", rrc.real());
}
qDebug() << "MainBench::testFIRRRCFilter: output samples written to test_firrrc.txt";
fclose(fd);
}