58 using std::setprecision;
72 void print_complex(
const Complex & z)
74 cout << fixed << setprecision(4) << setw(9) << z.real();
75 cout << (z.imag() < 0 ?
" - " :
" + ");
76 cout << fixed << setprecision(4) << setw(8) << std::abs(z.imag()) <<
"i";
83 void print_real_sequence(
const char * title,
const Array<T> & values)
85 cout << title <<
"\n";
86 for (
size_t i = 0; i < values.
size(); ++i)
87 cout <<
" [" << setw(2) << i <<
"] = "
88 << fixed << setprecision(4) << values[i] <<
"\n";
95 void print_complex_sequence(
const char * title,
const Array<Complex> & values)
97 cout << title <<
"\n";
98 for (
size_t i = 0; i < values.
size(); ++i)
100 cout <<
" [" << setw(2) << i <<
"] = ";
101 print_complex(values[i]);
115 void demo_real_signal_spectrum()
117 cout <<
"[1] Spectral analysis of a real signal\n";
118 cout <<
"Goal: Identify frequency components of a composite signal.\n";
121 constexpr size_t n = 10;
126 for (
size_t t = 0; t < n; ++t)
129 const double angle = 2.0 * std::numbers::pi *
static_cast<double>(t)
130 /
static_cast<double>(n);
133 const double sample = 1.50 * std::cos(angle)
134 + 0.75 * std::sin(3.0 * angle);
141 const auto spectrum = FFTD::spectrum(signal);
142 const auto compact_spectrum = FFTD::rfft(signal);
143 const auto restored_from_compact = FFTD::irfft(compact_spectrum, signal.
size());
144 const auto magnitudes = FFTD::magnitude_spectrum(spectrum);
145 const auto phases = FFTD::phase_spectrum(spectrum);
146 const auto hann_windowed = FFTD::windowed_spectrum(signal, FFTD::hann_window(signal.
size()));
147 const auto hann_magnitudes = FFTD::magnitude_spectrum(hann_windowed);
149 print_real_sequence(
"Time-domain samples x[t]:", signal);
151 cout <<
"Frequency bins X[k], magnitudes |X[k]| and phase arg(X[k]):\n";
152 for (
size_t k = 0;
k < spectrum.size(); ++
k)
154 cout <<
" k=" <<
k <<
" X[k] = ";
155 print_complex(spectrum[
k]);
156 cout <<
" |X[k]| = " << fixed << setprecision(4)
158 <<
" phase = " << phases[
k] <<
"\n";
161 cout <<
"\nCompact real spectrum R[k] (only 0..floor(N/2)):\n";
162 for (
size_t k = 0;
k < compact_spectrum.size(); ++
k)
164 cout <<
" k=" <<
k <<
" R[k] = ";
165 print_complex(compact_spectrum[
k]);
169 cout <<
"\nAnalysis:\n";
170 cout <<
"- Bin k=1 shows energy from 1.5 * cos(theta).\n";
171 cout <<
"- Bin k=3 shows energy from 0.75 * sin(3*theta).\n";
172 cout <<
"- For real signals, the spectrum is Hermitian symmetric:\n";
173 cout <<
" X[k] is the conjugate of X[N-k]. This is why |X[1]| = |X[9]|.\n";
174 cout <<
"- FFT<Real>::rfft() exposes only the non-redundant bins and\n";
175 cout <<
" FFT<Real>::irfft() reconstructs the original arbitrary-length signal.\n";
176 cout <<
" Compact round-trip sample 0 = " << restored_from_compact[0] <<
".\n";
177 cout <<
"- Applying FFT<Real>::windowed_spectrum(..., hann_window(N)) reduces\n";
178 cout <<
" edge discontinuity; here Hann-windowed |X[1]| = "
179 << hann_magnitudes[1] <<
".\n";
192 void demo_real_convolution()
194 cout <<
"[2] Real convolution via FFT\n";
195 cout <<
"Goal: Apply a smoothing filter to a sequence of samples.\n";
209 const auto filtered = FFTD::multiply(samples, kernel);
211 print_real_sequence(
"Original Samples:", samples);
212 print_real_sequence(
"Smoothing kernel (Filter):", kernel);
213 print_real_sequence(
"Filtered Result (Linear Convolution):", filtered);
215 cout <<
"This overload uses the optimized sequential path for real-valued inputs.\n";
216 cout <<
"For long streams, FFT<Real>::OverlapAdd or overlap_add_convolution()\n";
217 cout <<
"lets you reuse the filter spectrum block-by-block.\n";
218 cout <<
"Observation: The convolution result has size N + M - 1 = 6 + 3 - 1 = 8.\n";
219 cout <<
"The values at the edges reflect the ramp-up and ramp-down of the filter.\n";
233 void demo_complex_convolution()
235 cout <<
"[3] Complex polynomial multiplication\n";
236 cout <<
"Goal: Multiply two polynomials with complex coefficients.\n";
254 const auto c = FFTD::multiply(a, b);
256 print_complex_sequence(
"A(x) coefficients:", a);
257 print_complex_sequence(
"B(x) coefficients:", b);
258 print_complex_sequence(
"C(x) = A(x) * B(x):", c);
260 cout <<
"The result coefficients represent the new polynomial C(x).\n";
261 cout <<
"This shows how the FFT gracefully handles complex values natively.\n";
266 void demo_generic_containers()
268 cout <<
"[4] Generic iterable containers\n";
269 cout <<
"Goal: Use FFT directly with std::vector inputs.\n";
272 std::vector<double> real_signal = {1.0, 2.0, -1.0, 0.5, 3.0, -2.0, 4.0, -0.75};
273 const auto real_spectrum = FFTD::transform(real_signal);
275 std::vector<Complex> poly_a = {
280 std::vector<Complex> poly_b = {
285 const auto product = FFTD::multiply(poly_a, poly_b);
287 cout <<
"Input std::vector<double> size: " << real_signal.size() <<
"\n";
288 cout <<
"FFT(real_signal) returned an Aleph Array with "
289 << real_spectrum.size() <<
" frequency bins.\n\n";
291 print_complex_sequence(
"FFT(std::vector<double>) spectrum:", real_spectrum);
292 print_complex_sequence(
"FFT::multiply(std::vector<Complex>, std::vector<Complex>):",
295 cout <<
"No manual conversion to Array is needed: compatible iterable\n"
296 <<
"containers are accepted directly by FFT<Real>.\n";
301 void demo_concurrent_fft()
303 cout <<
"[5] Concurrent FFT with ThreadPool\n";
304 cout <<
"Goal: Keep the sequential and concurrent APIs explicitly separated.\n";
309 Array<double> signal = {0.5, -1.25, 3.5, 2.0, -0.75, 4.25, 1.0, -2.0};
320 const auto seq_spectrum = FFTD::transform(signal);
321 const auto par_spectrum = FFTD::ptransform(pool, signal);
322 const auto par_filtered = FFTD::pmultiply(pool, signal, kernel);
323 FFTD::Plan plan(complex_batch[0].
size());
324 const auto batch_spectra = plan.ptransformed_batch(pool, complex_batch,
false);
333 const auto image_spectrum = FFTD::transformed2d(image);
334 FFTD::OverlapAddBank overlap_bank(2, kernel, 4);
337 Array<double>({1.0, -0.5, 0.75, 2.0, -1.25, 0.5, 0.25, -0.75})
339 const auto overlap_batch = overlap_bank.pconvolve(pool, overlap_signals);
341 print_real_sequence(
"Input signal:", signal);
342 print_complex_sequence(
"Sequential FFT(signal):", seq_spectrum);
343 print_complex_sequence(
"Concurrent FFT(signal):", par_spectrum);
344 print_real_sequence(
"Concurrent convolution result:", par_filtered);
345 print_complex_sequence(
"Concurrent batch FFT(signal 0):", batch_spectra[0]);
346 print_complex_sequence(
"2-D FFT(image row 0):", image_spectrum[0]);
347 print_real_sequence(
"Concurrent overlap-add bank (channel 0):", overlap_batch[0]);
348 cout <<
"SIMD backend for FFT<double>: " << FFTD::simd_backend_name()
349 <<
" (batch-plan backend: " << FFTD::batched_plan_simd_backend_name()
350 <<
", detected hardware backend: "
351 << FFTD::detected_simd_backend_name()
352 <<
", policy: " << FFTD::simd_preference_name()
353 <<
", avx2 compiled/runtime: "
354 << (FFTD::avx2_kernel_compiled() ?
"yes" :
"no") <<
"/"
355 << (FFTD::avx2_runtime_available() ?
"yes" :
"no")
356 <<
", neon compiled/runtime: "
357 << (FFTD::neon_kernel_compiled() ?
"yes" :
"no") <<
"/"
358 << (FFTD::neon_runtime_available() ?
"yes" :
"no") <<
")\n";
360 cout <<
"Use FFT<Real>::transform()/multiply() for the sequential path and\n"
361 <<
"FFT<Real>::ptransform()/pmultiply() when you want explicit ThreadPool\n"
362 <<
"parallelism under Aleph's concurrency style. When many complex inputs\n"
363 <<
"share the same length, FFT<Real>::Plan::ptransformed_batch() reuses one\n"
364 <<
"plan and parallelizes across the batch instead of across a single FFT.\n"
365 <<
"For image-like data, transformed2d()/ptransformed2d() remove the need to\n"
366 <<
"manually loop over rows and columns, while transform_axes() targets flat\n"
367 <<
"buffers with explicit shape/stride metadata.\n"
368 <<
"For multichannel FIR streaming, FFT<Real>::OverlapAddBank shares the\n"
369 <<
"kernel spectrum across channels instead of rebuilding one convolver per\n"
375 void demo_stft_and_zero_phase_filtering()
377 cout <<
"[6] STFT, ISTFT, and zero-phase filtering\n";
378 cout <<
"Goal: Analyze by frames, reconstruct by overlap-add, inspect IIR response,\n"
379 <<
"stream STFT in chunks, and compare causal vs zero-phase filtering.\n";
384 0.0, 0.8, 1.0, 0.2, -0.7, -1.0, -0.3, 0.6,
389 FFTD::BiquadSection iir = {0.075, 0.15, 0.075, 1.0, -0.9, 0.2};
391 FFTD::STFTOptions stft_options;
392 stft_options.hop_size = 2;
393 stft_options.centered =
true;
394 stft_options.pad_end =
true;
395 stft_options.fft_size = 8;
396 stft_options.validate_nola =
true;
398 FFTD::ISTFTOptions istft_options;
399 istft_options.hop_size = 2;
400 istft_options.centered =
true;
401 istft_options.signal_length = signal.
size();
402 istft_options.validate_nola =
true;
404 const auto frames = FFTD::frame_signal(signal, analysis_window.
size(), 2,
true);
405 const auto spectrogram = FFTD::stft(signal, analysis_window, stft_options);
406 const auto parallel_spectrogram = FFTD::pstft(pool,
410 const auto reconstructed = FFTD::istft(spectrogram, analysis_window, istft_options);
411 const auto zero_phase_fir = FFTD::filtfilt(signal, fir, 4);
412 const auto zero_phase_iir = FFTD::filtfilt(signal, iir);
413 const auto designed_fir = FFTD::firwin_lowpass(33, 1200.0, 8000.0, 70.0);
417 const auto designed_firls =
418 FFTD::firls(33, firls_bands, firls_desired, 8000.0, firls_weights);
420 const auto designed_remez =
421 FFTD::remez(33, firls_bands, firls_desired, 8000.0, remez_weights);
422 const auto designed_bandpass =
423 FFTD::chebyshev2_bandpass(4, 40.0, 700.0, 1500.0, 8000.0);
424 const auto designed_elliptic =
425 FFTD::elliptic_bandstop(4, 1.0, 40.0, 700.0, 1500.0, 8000.0);
426 const auto response = FFTD::freqz(iir, 5);
427 const auto designed_firls_response = FFTD::freqz(designed_firls, 5,
false);
428 const auto designed_remez_response = FFTD::freqz(designed_remez, 5,
false);
429 const auto designed_bandpass_response = FFTD::freqz(designed_bandpass, 5);
430 const auto designed_elliptic_response = FFTD::freqz(designed_elliptic, 5);
431 const auto poles = FFTD::poles(iir);
432 const auto pairs = FFTD::pair_poles_and_zeros(iir);
433 const auto group = FFTD::group_delay(iir, 5);
434 const auto phase = FFTD::phase_delay(iir, 5);
435 const auto stability_margin = FFTD::stability_margin(iir);
436 const auto min_pair_distance = FFTD::minimum_pole_zero_distance(iir);
438 FFTD::STFTProcessor processor(analysis_window, stft_options);
439 FFTD::ISTFTProcessor inverse_processor(spectrogram[0].
size(),
445 FFTD::LFilter causal_iir(iir);
448 for (
size_t i = 0; i < signal.
size() / 2; ++i)
449 first_half.
append(signal[i]);
450 for (
size_t i = signal.
size() / 2; i < signal.
size(); ++i)
451 second_half.
append(signal[i]);
452 const auto streamed_first = processor.process_block(first_half);
453 const auto streamed_second = processor.process_block(second_half);
454 const auto streamed_tail = processor.flush();
455 const auto causal_first = causal_iir.filter(first_half);
456 const auto causal_second = causal_iir.filter(second_half);
458 for (
size_t i = 0; i < causal_first.size(); ++i)
459 causal_streamed.
append(causal_first[i]);
460 for (
size_t i = 0; i < causal_second.size(); ++i)
461 causal_streamed.
append(causal_second[i]);
462 for (
size_t i = 0; i < streamed_first.size(); ++i)
463 streamed_spectrogram.
append(streamed_first[i]);
464 for (
size_t i = 0; i < streamed_second.size(); ++i)
465 streamed_spectrogram.
append(streamed_second[i]);
466 for (
size_t i = 0; i < streamed_tail.size(); ++i)
467 streamed_spectrogram.
append(streamed_tail[i]);
471 for (
size_t i = 0; i < spectrogram.size() / 2; ++i)
472 first_frames.
append(spectrogram[i]);
473 for (
size_t i = spectrogram.size() / 2; i < spectrogram.size(); ++i)
474 second_frames.
append(spectrogram[i]);
475 const auto reconstructed_first = inverse_processor.process_block(first_frames);
476 const auto reconstructed_second = inverse_processor.process_block(second_frames);
477 const auto reconstructed_tail = inverse_processor.flush();
478 for (
size_t i = 0; i < reconstructed_first.size(); ++i)
479 streamed_reconstruction.
append(reconstructed_first[i]);
480 for (
size_t i = 0; i < reconstructed_second.size(); ++i)
481 streamed_reconstruction.
append(reconstructed_second[i]);
482 for (
size_t i = 0; i < reconstructed_tail.size(); ++i)
483 streamed_reconstruction.
append(reconstructed_tail[i]);
486 const auto batched = FFTD::batched_stft(batch_signals,
489 const auto frame_major =
490 FFTD::multichannel_stft(batch_signals,
493 FFTD::SpectrogramLayout::frame_channel_bin);
494 const auto multichannel_reconstructed =
495 FFTD::multichannel_istft(frame_major,
500 FFTD::SpectrogramLayout::frame_channel_bin);
502 for (
size_t i = 0; i < std::min<size_t>(6, designed_fir.size()); ++i)
503 designed_fir_preview.
append(designed_fir[i]);
505 for (
size_t i = 0; i < std::min<size_t>(6, designed_firls.size()); ++i)
506 designed_firls_preview.
append(designed_firls[i]);
508 for (
size_t i = 0; i < std::min<size_t>(6, designed_remez.size()); ++i)
509 designed_remez_preview.
append(designed_remez[i]);
511 print_real_sequence(
"Input signal:", signal);
512 cout <<
"Raw frame_signal frames: " << frames.size()
513 <<
", centered STFT frames: " << spectrogram.size()
514 <<
", FFT bins per frame: " << spectrogram[0].size() <<
"\n";
515 cout <<
"Parallel STFT frames: " << parallel_spectrogram.size()
516 <<
", streamed STFT frames: " << streamed_spectrogram.
size()
517 <<
", streamed ISTFT samples: " << streamed_reconstruction.
size()
518 <<
", batched STFT items: " << batched.size()
519 <<
", frame-major multichannel frames: " << frame_major.size() <<
"\n";
520 print_real_sequence(
"ISTFT reconstruction:", reconstructed);
521 print_real_sequence(
"Streamed ISTFT reconstruction:", streamed_reconstruction);
522 print_real_sequence(
"Causal streamed IIR result:", causal_streamed);
523 print_real_sequence(
"Zero-phase FIR result:", zero_phase_fir);
524 print_real_sequence(
"Zero-phase IIR result:", zero_phase_iir);
525 print_real_sequence(
"firwin_lowpass taps (first 6):", designed_fir_preview);
526 print_real_sequence(
"firls low-pass taps (first 6):", designed_firls_preview);
527 print_real_sequence(
"remez low-pass taps (first 6):", designed_remez_preview);
528 print_real_sequence(
"Frame-major multichannel ISTFT(channel 0):",
529 multichannel_reconstructed[0]);
530 print_complex_sequence(
"STFT frame 0:", spectrogram[0]);
531 print_complex_sequence(
"STFT frame 1:", spectrogram[1]);
532 print_complex_sequence(
"IIR poles:", poles);
533 print_complex_sequence(
"Chebyshev-II band-pass response samples:",
534 designed_bandpass_response.response);
535 print_complex_sequence(
"Elliptic band-stop response samples:",
536 designed_elliptic_response.response);
537 print_complex_sequence(
"firls low-pass response samples:",
538 designed_firls_response.response);
539 print_complex_sequence(
"remez low-pass response samples:",
540 designed_remez_response.response);
541 cout <<
"Pole/zero pairs tracked: " << pairs.size() <<
"\n";
542 print_real_sequence(
"Estimated group delay:", group);
543 print_real_sequence(
"Estimated phase delay:", phase);
544 cout <<
"IIR |H(0)| = " << std::abs(response.response[0])
545 <<
", |H(pi)| = " << std::abs(response.response[response.response.size() - 1])
546 <<
", stable = " << (FFTD::is_stable(iir) ?
"yes" :
"no")
547 <<
", stability margin = " << stability_margin
548 <<
", min pole/zero distance = " << min_pair_distance
551 cout <<
"frame_signal() exposes the hop-size layout explicitly, stft() applies\n"
552 <<
"windowing + zero-padding per frame, pstft() parallelizes the same analysis,\n"
553 <<
"STFTProcessor and ISTFTProcessor keep chunked analysis/synthesis aligned\n"
554 <<
"with the offline result, the centered STFT/ISTFT option pair reconstructs\n"
555 <<
"the original signal exactly under NOLA, batched_stft() scales the API to\n"
556 <<
"multiple signals, multichannel_stft()/multichannel_istft() switch between\n"
557 <<
"channel-major and frame-major layouts without manual transposes, and\n"
558 <<
"freqz() plus poles()/pair_poles_and_zeros(),\n"
559 <<
"stability_margin(), group_delay()/phase_delay() expose filter behavior.\n"
560 <<
"The same header also covers FIR design through firwin_*(), firls(), remez(),\n"
561 <<
"and reusable Chebyshev-II plus elliptic/Cauer SOS designs.\n";
566 void demo_production_dsp_utilities()
568 cout <<
"[7] Production DSP utilities\n";
569 cout <<
"Goal: Estimate PSD/coherence, resample by rational factors, and\n"
570 <<
"compare overlap-save with low-latency partitioned convolution.\n";
573 constexpr double sample_rate = 8000.0;
578 for (
size_t i = 0; i < 256; ++i)
580 const double t =
static_cast<double>(i) / sample_rate;
581 signal.
append(std::sin(2.0 * std::numbers::pi * 1000.0 * t)
582 + 0.35 * std::sin(2.0 * std::numbers::pi * 1800.0 * t));
583 shifted_signal.
append(1.5 * std::sin(2.0 * std::numbers::pi * 1000.0 * t + 0.25)
584 + 0.35 * std::sin(2.0 * std::numbers::pi * 1800.0 * t));
587 const auto window = FFTD::hann_window(64);
588 FFTD::WelchOptions welch_options;
589 welch_options.hop_size = 32;
590 welch_options.fft_size = 128;
592 const auto psd = FFTD::welch(signal, window, sample_rate, welch_options);
593 const auto cross = FFTD::csd(signal, shifted_signal, window, sample_rate, welch_options);
594 const auto coh = FFTD::coherence(signal, shifted_signal, window, sample_rate, welch_options);
597 for (
size_t i = 1; i < psd.density.size(); ++i)
598 if (psd.density[i] > psd.density[peak])
603 const auto upfirdn_result = FFTD::upfirdn(prototype, kernel, 3, 2);
604 const auto resampled = FFTD::resample_poly(signal, 3, 2);
606 FFTD::OverlapSave overlap_save(kernel, 16);
608 const auto overlap_first =
610 const auto overlap_second =
611 overlap_save.process_block(
Array<double>({2.0, 0.25, -0.5, 1.0}));
612 const auto overlap_tail = overlap_save.flush();
613 for (
size_t i = 0; i < overlap_first.size(); ++i)
614 overlap_save_streamed.
append(overlap_first[i]);
615 for (
size_t i = 0; i < overlap_second.size(); ++i)
616 overlap_save_streamed.
append(overlap_second[i]);
617 for (
size_t i = 0; i < overlap_tail.size(); ++i)
618 overlap_save_streamed.
append(overlap_tail[i]);
620 FFTD::PartitionedConvolver partitioned(kernel, 8);
622 const auto partitioned_first =
624 const auto partitioned_second =
625 partitioned.process_block(
Array<double>({2.0, 0.25, -0.5, 1.0}));
626 const auto partitioned_tail = partitioned.flush();
627 for (
size_t i = 0; i < partitioned_first.size(); ++i)
628 partitioned_streamed.
append(partitioned_first[i]);
629 for (
size_t i = 0; i < partitioned_second.size(); ++i)
630 partitioned_streamed.
append(partitioned_second[i]);
631 for (
size_t i = 0; i < partitioned_tail.size(); ++i)
632 partitioned_streamed.
append(partitioned_tail[i]);
635 for (
size_t i = 0; i < std::min<size_t>(10, resampled.size()); ++i)
636 resampled_preview.
append(resampled[i]);
638 for (
size_t i = 0; i < std::min<size_t>(10, upfirdn_result.size()); ++i)
639 upfirdn_preview.
append(upfirdn_result[i]);
641 print_real_sequence(
"upfirdn prototype result (first 10):", upfirdn_preview);
642 print_real_sequence(
"resample_poly(signal, 3/2) (first 10):", resampled_preview);
643 print_real_sequence(
"Overlap-save streaming result:", overlap_save_streamed);
644 print_real_sequence(
"Partitioned streaming result:", partitioned_streamed);
646 cout <<
"Welch peak frequency: " << psd.frequency[peak]
647 <<
" Hz, PSD = " << psd.density[peak] <<
"\n";
648 cout <<
"Cross-spectrum magnitude at that bin: "
649 << std::abs(cross.density[peak])
650 <<
", coherence = " << coh.magnitude_squared[peak] <<
"\n";
651 cout <<
"Window coherent gain = " << FFTD::window_coherent_gain(window)
652 <<
", ENBW = " << FFTD::window_enbw(window)
653 <<
", frame count (exact) = "
654 << FFTD::frame_offsets(signal.
size(), window.size(), welch_options.hop_size,
false).size()
657 cout <<
"welch()/csd()/coherence() provide one-sided spectral estimators,\n"
658 <<
"upfirdn()/resample_poly() cover rational-rate sample conversion,\n"
659 <<
"overlap_save_convolution() targets block FIR filtering, and\n"
660 <<
"PartitionedConvolver keeps latency tied to the configured partition size.\n";
671 std::cout <<
"\n=== Aleph-w: Fast Fourier Transform (FFT) Demonstration ===\n\n";
674 demo_real_signal_spectrum();
677 demo_real_convolution();
680 demo_complex_convolution();
683 demo_generic_containers();
686 demo_concurrent_fft();
689 demo_stft_and_zero_phase_filtering();
692 demo_production_dsp_utilities();
694 std::cout <<
"All demonstrations completed successfully.\n";
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Fast Fourier Transform (FFT) and DSP Toolkit.
A reusable thread pool for efficient parallel task execution.
Fast Fourier Transform (FFT) and Digital Signal Processing (DSP) toolkit.
int main()
Main entry point.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
void print_rule()
Prints a horizontal rule for example output separation.
Dynamic array container with automatic resizing.