FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
waveform_factory.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2006-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file waveform_factory.cpp
10 * @brief Implementation for loading waveform data into RadarSignal objects.
11 */
12
13#include "waveform_factory.h"
14
15#include <complex>
16#include <cstddef>
17#include <filesystem>
18#include <fstream>
19#include <span>
20#include <stdexcept>
21#include <string_view>
22#include <utility>
23#include <vector>
24
25#include "core/config.h"
26#include "core/parameters.h"
27#include "hdf5_handler.h"
28#include "signal/radar_signal.h"
29
32
33namespace
34{
35 /**
36 * @brief Loads a radar waveform from an HDF5 file and returns a RadarSignal object.
37 *
38 * @param name The name of the radar signal.
39 * @param filepath The path to the HDF5 file containing the waveform data.
40 * @param power The power of the radar signal in the waveform.
41 * @param carrierFreq The carrier frequency of the radar signal.
42 * @return A unique pointer to a RadarSignal object loaded with the waveform data.
43 * @throws std::runtime_error If the file cannot be opened or the file format is unrecognized.
44 */
45 std::unique_ptr<RadarSignal> loadWaveformFromHdf5File(const std::string& name,
46 const std::filesystem::path& filepath, const RealType power,
47 const RealType carrierFreq)
48 {
49 std::vector<ComplexType> data;
50 serial::readPulseData(filepath.string(), data);
51
52 auto signal = std::make_unique<Signal>();
53 signal->load(data, data.size(), params::rate());
54 return std::make_unique<RadarSignal>(name, power, carrierFreq,
55 static_cast<RealType>(data.size()) / params::rate(), std::move(signal));
56 }
57
58 /**
59 * @brief Loads a radar waveform from a CSV file and returns a RadarSignal object.
60 *
61 * @param name The name of the radar signal.
62 * @param filepath The path to the CSV file containing the waveform data.
63 * @param power The power of the radar signal in the waveform.
64 * @param carrierFreq The carrier frequency of the radar signal.
65 * @return A unique pointer to a RadarSignal object loaded with the waveform data.
66 * @throws std::runtime_error If the file cannot be opened or the file format is unrecognized.
67 */
68 std::unique_ptr<RadarSignal> loadWaveformFromCsvFile(const std::string& name, const std::filesystem::path& filepath,
69 const RealType power, const RealType carrierFreq)
70 {
71 std::ifstream ifile(filepath);
72 if (!ifile)
73 {
74 LOG(logging::Level::FATAL, "Could not open file '{}' to read waveform", filepath.string());
75 throw std::runtime_error("Could not open file '" + filepath.string() + "' to read waveform");
76 }
77
78 RealType rlength, rate;
79 ifile >> rlength >> rate;
80
81 const auto length = static_cast<std::size_t>(rlength);
82 std::vector<ComplexType> data(length);
83
84 // Read the file data
85 for (std::size_t done = 0; done < length && ifile >> data[done]; ++done)
86 {
87 }
88
89 if (ifile.fail() || data.size() != length)
90 {
91 LOG(logging::Level::FATAL, "Could not read full waveform from file '{}'", filepath.string());
92 throw std::runtime_error("Could not read full waveform from file '" + filepath.string() + "'");
93 }
94
95 auto signal = std::make_unique<Signal>();
96 signal->load(data, length, rate);
97 return std::make_unique<RadarSignal>(name, power, carrierFreq, rlength / rate, std::move(signal));
98 }
99
100 /**
101 * @brief Checks if a filename has a specific extension.
102 *
103 * @param filename The filename to check.
104 * @param ext The extension to check for.
105 * @return True if the filename has the specified extension, false otherwise.
106 */
107 constexpr bool hasExtension(const std::string_view filename, const std::string_view ext) noexcept
108 {
109 return filename.ends_with(ext);
110 }
111}
112
113namespace serial
114{
115 std::unique_ptr<RadarSignal> loadWaveformFromFile(const std::string& name, const std::string& filename,
116 const RealType power, const RealType carrierFreq)
117 {
118 const std::filesystem::path filepath = filename;
119 const auto extension = filepath.extension().string();
120
121 if (hasExtension(extension, ".csv"))
122 {
123 auto wave = loadWaveformFromCsvFile(name, filepath, power, carrierFreq);
124 wave->setFilename(filename);
125 return wave;
126 }
127 if (hasExtension(extension, ".h5"))
128 {
129 auto wave = loadWaveformFromHdf5File(name, filepath, power, carrierFreq);
130 wave->setFilename(filename);
131 return wave;
132 }
133
134 LOG(logging::Level::FATAL, "Unrecognized file extension '{}' for file: '{}'", extension, filename);
135 throw std::runtime_error("Unrecognized file extension '" + extension + "' for file: " + filename);
136 }
137}
Class representing a radar signal with associated properties.
Class for handling radar waveform signal data.
Global configuration file for the project.
double RealType
Type for real numbers.
Definition config.h:27
Header file for HDF5 data export and import functions.
#define LOG(level,...)
Definition logging.h:19
@ FATAL
Fatal level for severe error events.
RealType rate() noexcept
Get the rendering sample rate.
Definition parameters.h:109
std::unique_ptr< RadarSignal > loadWaveformFromFile(const std::string &name, const std::string &filename, const RealType power, const RealType carrierFreq)
Loads a radar waveform from a file and returns a RadarSignal object.
void readPulseData(const std::string &name, std::vector< ComplexType > &data)
Reads pulse data from an HDF5 file.
Defines the Parameters struct and provides methods for managing simulation parameters.
Classes for handling radar waveforms and signals.
Interface for loading waveform data into RadarSignal objects.