Skip to content

Instantly share code, notes, and snippets.

@higuoxing
Last active June 5, 2021 15:00
Show Gist options
  • Save higuoxing/1c520175ed42e57f8bc601ab5ba6723b to your computer and use it in GitHub Desktop.
Save higuoxing/1c520175ed42e57f8bc601ab5ba6723b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#include <complex>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <iostream>
constexpr double PI = 3.1415926535897;
template <typename T>
class wave_table_t {
public:
wave_table_t(const std::string &wave_type, size_t len, const T ampl)
: _wave_table(len) {
if (wave_type == "SINE") {
static T tau = 2 * PI;
static const std::complex<T> J(0, 1);
for (size_t I = 0; I < len; ++I) {
_wave_table[I] = ampl * std::exp(J * static_cast<T>(tau * I / len));
}
_power_dbfs = static_cast<T>(20 * std::log10(ampl));
}
}
void dump_to_file(const std::string &filename) const {
std::ofstream ofile(filename.c_str(), std::ofstream::binary);
ofile.write((char *)&_wave_table[0], _wave_table.size() * sizeof(std::complex<T>));
ofile.close();
}
private:
T _power_dbfs;
std::vector<std::complex<T>> _wave_table;
};
int main() {
wave_table_t<float> wt("SINE", 1024, 1.0);
wt.dump_to_file("/tmp/mysine.data");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment