Merge pull request #1 from Lauchmelder23/development

Development
This commit is contained in:
Lauchmelder 2021-04-28 23:41:24 +02:00 committed by GitHub
commit ab874eb553
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 7 deletions

View file

@ -49,7 +49,12 @@ std::vector<std::complex<double>> radix2dit(const std::vector<double>::const_ite
return output;
}
std::vector<std::pair<double, double>> FFT(const std::vector<double>::const_iterator& begin, const std::vector<double>::const_iterator& end, size_t sampleRate)
std::vector<std::pair<double, double>>
FFT(const std::vector<double>::const_iterator& begin,
const std::vector<double>::const_iterator& end,
size_t sampleRate,
double minFreq, double maxFreq,
unsigned int zeropadding)
{
std::vector<double> signal(begin, end);
size_t N = signal.size();
@ -60,13 +65,21 @@ std::vector<std::pair<double, double>> FFT(const std::vector<double>::const_iter
N++;
}
if (zeropadding > 1) {
N = (signal.size() << (zeropadding - 1));
signal.insert(signal.end(), N - signal.size(), 0);
}
std::vector<std::complex<double>> spectrum = radix2dit(signal.cbegin(), N, 1);
double freqRes = (double)sampleRate / (double)N;
double nyquistLimit = (double)sampleRate / 2.0f;
std::vector<std::pair<double, double>> output;
double freq = 0.0f;
for (int k = 0; freq < nyquistLimit; k++)
double freq = minFreq;
if (maxFreq == 0)
maxFreq = nyquistLimit;
for (int k = freq / freqRes; freq < nyquistLimit && freq < maxFreq; k++)
{
output.push_back(std::make_pair(freq, 2.0f * std::abs(spectrum[k]) / (double)N));
freq += freqRes;

View file

@ -14,6 +14,9 @@ struct Settings {
std::vector<std::filesystem::path> files;
bool quiet;
float splitInterval;
double minFreq, maxFreq;
unsigned int analyzeChannel;
unsigned int zeropadding;
};
Settings Parse(int argc, char** argv);
@ -38,6 +41,12 @@ int main(int argc, char** argv)
int numChannels = audioFile.getNumChannels();
nlohmann::json output;
int c = setts.analyzeChannel;
if (c == 0)
c = 1;
else
numChannels = c;
for (int c = 1; c <= numChannels; c++) {
PRINTER(setts, "\rAnalyzing " << filename << "... Channel " << c << "/" << numChannels << " 0% ");
@ -46,7 +55,14 @@ int main(int argc, char** argv)
if (setts.splitInterval == 0.0f)
{
std::vector<std::pair<double, double>> spectrum = FFT(audioFile.samples[c-1].cbegin(), audioFile.samples[c-1].cend(), sampleRate);
std::vector<std::pair<double, double>> spectrum =
FFT(
audioFile.samples[c-1].cbegin(),
audioFile.samples[c-1].cend(),
sampleRate,
setts.minFreq, setts.maxFreq,
setts.zeropadding
);
output[chName] = nlohmann::json::array();
for (const std::pair<double, double>& pair : spectrum) {
@ -64,8 +80,11 @@ int main(int argc, char** argv)
audioFile.samples[c-1].cbegin() + currentSample,
std::min(
audioFile.samples[c-1].cbegin() + currentSample + sampleInterval,
audioFile.samples[c-1].cend()),
sampleRate
audioFile.samples[c-1].cend()
),
sampleRate,
setts.minFreq, setts.maxFreq,
setts.zeropadding
);
output[chName].push_back({
@ -102,10 +121,13 @@ Settings Parse(int argc, char** argv)
cxxopts::Options options("spectralyze", "Fourier transforms audio files");
options
.set_width(70)
.positional_help("file1 [file2...]")
.positional_help("FILE1 [FILE2...]")
.add_options()
("q,quiet", "Suppress text output", cxxopts::value<bool>()->default_value("false"))
("i,interval", "Splits audio file into intervals of length i milliseconds and transforms them individually (0 to not split file)", cxxopts::value<float>())
("f,frequency", "Defines the frequency range of the output spectrum (Default: all the frequencies)", cxxopts::value<std::vector<double>>())
("p,pad", "Add extra zero-padding. By default, the program will pad the signals with 0s until the number of samples is a power of 2 (this would be equivalent to -p 1). With this option you can tell the program to instead pad until the power of 2 after the next one (-p 2) etc. This increases frequency resolution", cxxopts::value<unsigned int>())
("m,mono", "Analyze only the given channel", cxxopts::value<unsigned int>()->default_value("0"))
("files", "Files to fourier transform", cxxopts::value<std::vector<std::filesystem::path>>())
("h,help", "Print usage")
;
@ -119,6 +141,17 @@ Settings Parse(int argc, char** argv)
exit(0);
}
if (!result.count("frequency"))
{
setts.minFreq = 0.0f;
setts.maxFreq = 0.0f;
}
else
{
setts.minFreq = result["frequency"].as<std::vector<double>>()[0];
setts.maxFreq = result["frequency"].as<std::vector<double>>()[1];
}
if (!result.count("files"))
{
std::cerr << "At least one positional argument is required." << std::endl;
@ -128,6 +161,15 @@ Settings Parse(int argc, char** argv)
setts.files = result["files"].as<std::vector<std::filesystem::path>>();
setts.quiet = (result.count("quiet") ? result["quiet"].as<bool>() : false);
setts.splitInterval = (result.count("interval") ? result["interval"].as<float>() : 0.0f);
setts.analyzeChannel = (result.count("mono") ? result["mono"].as<unsigned int>() : 0);
setts.zeropadding = (result.count("pad") ? result["pad"].as<unsigned int>() : 1);
if (setts.maxFreq <= setts.minFreq && (setts.maxFreq != 0))
{
std::cerr << "Maximum frequency cannot be smaller than minimum frequency" << std::endl;
exit(1);
}
}
catch (const cxxopts::OptionException& e)
{