Skip to content

Commit

Permalink
More detailed comments for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dancazarin committed Aug 9, 2024
1 parent 5c37b7b commit 5694629
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 94 deletions.
134 changes: 113 additions & 21 deletions examples/biquads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,101 +14,193 @@ using namespace kfr;

int main()
{
// Print the version of the KFR library being used
println(library_version());

// Define options for plotting DSP data
const std::string options = "phaseresp=True";

// Define a buffer for storing the filter output
univector<fbase, 128> output;

// --------------------------------------------------------------------------------------
// ------------------------- Biquad Notch Filters Example -------------------------------
// --------------------------------------------------------------------------------------
{
biquad_section<fbase> bq[] = { biquad_notch(0.1, 0.5), biquad_notch(0.2, 0.5), biquad_notch(0.3, 0.5),
biquad_notch(0.4, 0.5) };
output = iir(unitimpulse(), iir_params{ bq });
// Initialize an array of biquad notch filters with different center frequencies
// biquad_notch(frequency, Q) where the frequency is relative to the sample rate
biquad_section<fbase> bq[] = {
biquad_notch(0.1, 0.5),
biquad_notch(0.2, 0.5),
biquad_notch(0.3, 0.5),
biquad_notch(0.4, 0.5),
};
// Apply the biquad filters to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter responses
plot_save("biquad_notch", output, options + ", title='Four Biquad Notch filters'");

// --------------------------------------------------------------------------------------
// ------------------------- Biquad Lowpass Filter Example ------------------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad lowpass filter with specific parameters
// biquad_lowpass(frequency, Q) where the frequency is relative to the sample rate
biquad_section<fbase> bq[] = { biquad_lowpass(0.2, 0.9) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad lowpass filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_lowpass", output, options + ", title='Biquad Low pass filter (0.2, 0.9)'");

// --------------------------------------------------------------------------------------
// ------------------------- Biquad Highpass Filter Example -----------------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad highpass filter with specific parameters
// biquad_highpass(frequency, Q) where the frequency is relative to the sample rate
biquad_section<fbase> bq[] = { biquad_highpass(0.3, 0.1) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad highpass filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_highpass", output, options + ", title='Biquad High pass filter (0.3, 0.1)'");

// --------------------------------------------------------------------------------------
// -------------------------- Biquad Peak Filter Example --------------------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad peak filter with specific parameters
// biquad_peak(frequency, Q, gain) where the frequency is relative to the sample rate and the gain is
// in decibels
biquad_section<fbase> bq[] = { biquad_peak(0.3, 0.5, +9.0) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad peak filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_peak", output, options + ", title='Biquad Peak filter (0.2, 0.5, +9)'");

// --------------------------------------------------------------------------------------
// -------------------------- Biquad Peak Filter Example (2) ----------------------------
// --------------------------------------------------------------------------------------
{
// Initialize another biquad peak filter with different parameters
// biquad_peak(frequency, Q, gain) where the frequency is relative to the sample rate and the gain is
// in decibels
biquad_section<fbase> bq[] = { biquad_peak(0.3, 3.0, -2.0) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad peak filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_peak2", output, options + ", title='Biquad Peak filter (0.3, 3, -2)'");

// --------------------------------------------------------------------------------------
// -------------------------- Biquad Low Shelf Filter Example ---------------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad low shelf filter with specific parameters
// biquad_lowshelf(frequency, gain) where the frequency is relative to the sample rate and the gain is
// in decibels
biquad_section<fbase> bq[] = { biquad_lowshelf(0.3, -1.0) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad low shelf filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_lowshelf", output, options + ", title='Biquad low shelf filter (0.3, -1)'");

// --------------------------------------------------------------------------------------
// -------------------------- Biquad High Shelf Filter Example --------------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad high shelf filter with specific parameters
// biquad_highshelf(frequency, gain) where the frequency is relative to the sample rate and the gain
// is in decibels
biquad_section<fbase> bq[] = { biquad_highshelf(0.3, +9.0) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad high shelf filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_highshelf", output, options + ", title='Biquad high shelf filter (0.3, +9)'");

// --------------------------------------------------------------------------------------
// ------------------------- Biquad Bandpass Filter Example -----------------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad bandpass filter with specific parameters
// biquad_bandpass(frequency, Q) where the frequency is relative to the sample rate
biquad_section<fbase> bq[] = { biquad_bandpass(0.25, 0.2) };
output = iir(unitimpulse(), iir_params{ bq });
// Apply the biquad bandpass filter to a unit impulse signal and store the result in 'output'
output = iir(unitimpulse(), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_bandpass", output, options + ", title='Biquad band pass (0.25, 0.2)'");

// --------------------------------------------------------------------------------------
// ----------------- Biquad Bandpass Filter Example with std::vector --------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad bandpass filter with specific parameters
biquad_section<fbase> bq[] = { biquad_bandpass(0.25, 0.2) };
// Create a std::vector for the input data and set the first element to 1 (unit impulse)
std::vector<fbase> data(output.size(), 0.f);
data[0] = 1.f;
output = iir(make_univector(data), iir_params{ bq });
// Apply the biquad bandpass filter to the input data and store the result in 'output'
output = iir(make_univector(data), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_bandpass_stdvector", output, options + ", title='Biquad band pass (0.25, 0.2)'");

// --------------------------------------------------------------------------------------
// ------------------- Biquad Bandpass Filter Example with C array ----------------------
// --------------------------------------------------------------------------------------
{
// Initialize a biquad bandpass filter with specific parameters
biquad_section<fbase> bq[] = { biquad_bandpass(0.25, 0.2) };
fbase data[output.size()] = { 0 }; // .size() is constexpr
data[0] = 1.f;
output = iir(make_univector(data), iir_params{ bq });
// Create a C array for the input data and set the first element to 1 (unit impulse)
fbase data[output.size()] = { 0 }; // .size() is constexpr
data[0] = 1.f;
// Apply the biquad bandpass filter to the input data and store the result in 'output'
output = iir(make_univector(data), iir_params{ bq });
}
// Save the plot of the filter response
plot_save("biquad_bandpass_carray", output, options + ", title='Biquad band pass (0.25, 0.2)'");

// --------------------------------------------------------------------------------------
// ----------------- Custom Biquad Lowpass Filter using expression_filter ---------------
// --------------------------------------------------------------------------------------
{
// filter initialization
biquad_section<fbase> bq[] = { biquad_lowpass(0.2, 0.9) };
// Initialize a biquad lowpass filter with specific parameters
biquad_section<fbase> bq[] = { biquad_lowpass(0.2, 0.9) };
// Create a type-erased expression filter for the biquad lowpass filter
expression_filter<fbase> filter = to_filter(iir(placeholder<fbase>(), iir_params{ bq }));

// prepare data
// Prepare a unit impulse signal
output = unitimpulse();

// apply filter
// Apply the expression filter to the unit impulse signal
filter.apply(output);
}
// Save the plot of the filter response
plot_save("biquad_custom_filter_lowpass", output,
options + ", title='Biquad Low pass filter (0.2, 0.9) (using expression_filter)'");

// --------------------------------------------------------------------------------------
// ---------------------- Custom Biquad Lowpass Filter using iir_filter -----------------
// --------------------------------------------------------------------------------------
{
// filter initialization
// Initialize a biquad lowpass filter with specific parameters
biquad_section<fbase> bq[] = { biquad_lowpass(0.2, 0.9) };
// Create an IIR filter for the biquad lowpass filter
iir_filter<fbase> filter(bq);

// prepare data
// Prepare a unit impulse signal
output = unitimpulse();

// apply filter
// Apply the IIR filter to the unit impulse signal
filter.apply(output);
}
// Save the plot of the filter response
plot_save("biquad_filter_lowpass", output,
options + ", title='Biquad Low pass filter (0.2, 0.9) (using iir_filter)'");

Expand Down
23 changes: 16 additions & 7 deletions examples/dft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,48 @@ using namespace kfr;

int main()
{
// Print the version of the KFR library being used
println(library_version());

// fft size
// Define the size of the Fast Fourier Transform (FFT)
const size_t size = 128;

// initialize input & output buffers
// Initialize input and output buffers
// 'in' buffer is filled with a sine wave spanning 4 cycles over the given range
// 'out' buffer is initialized with 'qnan' (quiet NaN) to represent uninitialized state
univector<complex<fbase>, size> in = sin(linspace(0.0, c_pi<fbase, 2> * 4.0, size));
univector<complex<fbase>, size> out = scalar(qnan);

// initialize fft
// Create an FFT plan for the defined size
const dft_plan<fbase> dft(size);

// Dump the details of the FFT plan (for debugging or information purposes)
dft.dump();

// allocate work buffer for fft (if needed)
// Allocate a temporary buffer for the FFT computation if needed
univector<u8> temp(dft.temp_size);

// perform forward fft
// Perform the forward FFT on the input buffer 'in', store the result in the 'out' buffer
dft.execute(out, in, temp);

// scale output
// Scale the output of the FFT by dividing by the size
out = out / size;

// get magnitude and convert to decibels
// Convert the amplitude of the FFT output to decibels (dB)
// 'cabs' computes the magnitude of the complex numbers in 'out'
// 'amp_to_dB' converts the amplitude values to decibel scale
univector<fbase, size> dB = amp_to_dB(cabs(out));

// Print the maximum, minimum, mean, and root mean square (RMS) of the dB values
println("max = ", maxof(dB));
println("min = ", minof(dB));
println("mean = ", mean(dB));
println("rms = ", rms(dB));

// Print the input buffer 'in'
println(in);
println();
// Print the dB values
println(dB);
return 0;
}
Loading

0 comments on commit 5694629

Please sign in to comment.