Re: [LAD] Converting single-cycle wave to harmonic spectrum
Hi Fons, thanks again. Now it's all clear. I should have remember about the sine and cosine thing. :( May 10 2021, Fons Adriaensen has written: * the amplitude is hypot (real, imag). * the phase is atan2 (imag, real). This is really helpful! Best wishes, Jeanette Ciao, -- FA -- * Website: http://juliencoder.de - for summer is a state of sound * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g * Audiobombs: https://www.audiobombs.com/users/jeanette_c * GitHub: https://github.com/jeanette-c But tell me what happens when it stops? <3 (Britney Spears) ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
On Mon, May 10, 2021 at 11:00:10PM +0200, Jeanette C. wrote: > The output can be read as two halves: > up to half the array: > output[k] = real[k] the real part of the FFT > The second half: > output[size -k] = imaginary[k], the imaginary/complex part of the FFT > The output shows 0 for the first to real values, but the same strength for > the first two imaginary parts. Is this to be expected? Yes. The sin() waves appear in the imaginary part, if you would have used cos() instead that would show up in the real part. In general you will have both, and * the amplitude is hypot (real, imag). * the phase is atan2 (imag, real). Ciao, -- FA ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
Hi Fons and you other FFT initiates, I have written a simple test program (end of this mail) that produces two sine waves one octave apart. I used the FFTW_R2HC plan, which operates on two double arrays. The output can be read as two halves: up to half the array: output[k] = real[k] the real part of the FFT The second half: output[size -k] = imaginary[k], the imaginary/complex part of the FFT The output shows 0 for the first to real values, but the same strength for the first two imaginary parts. Is this to be expected? Best wishes (code below), Jeanette *** fft_test.cpp *** // g++ -o fft_test fft_test.cpp -lfftw3 -lm #include #include #include using namespace std; int main() { // Create input, output arrays and allocate them double *in, *out; in = (double *) fftw_malloc(sizeof(double) * 2048); out = (double *) fftw_malloc(sizeof(double) * 2048); // Create the fftw plan: halfcomplex out[k] = real[k] out[size-k] = im[k] fftw_plan conv_plan = fftw_plan_r2r_1d(2048, in, out, FFTW_R2HC, FFTW_ESTIMATE); // Create input, two sinewaves one octave apart for (int i = 0;i<2048;i++) { in[i] = sin(M_PI * i/1024) + sin(M_PI * i/512); } // Now execute the FFT on the in-array fftw_execute(conv_plan); // "beautify" the output, by setting everything below 10^(-10) to zero for (int i = 0;i<2048;i++) { if (abs(out[i]) < 1e-10) { out[i] = 0; } } // Print the first 16 harmonics, both real and imaginary parts (see // half-complex fftw plan FFTW_R2HC) for (int i = 0;i<16;i++) { cout << "Harm[" << i << "] = " << out[i] << ", " << out[2047 - i] << endl; } // Free everything fftw_destroy_plan(conv_plan); fftw_free(in); fftw_free(out); return 0; } *** end of file *** -- * Website: http://juliencoder.de - for summer is a state of sound * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g * Audiobombs: https://www.audiobombs.com/users/jeanette_c * GitHub: https://github.com/jeanette-c But tell me what happens when it stops? <3 (Britney Spears) ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
Hi Fons, many thanks again, I already have the documentation for fftw open in my browser. It doesn't look complicated at all for such a simple case. Best wishes, Jeanette -- * Website: http://juliencoder.de - for summer is a state of sound * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g * Audiobombs: https://www.audiobombs.com/users/jeanette_c * GitHub: https://github.com/jeanette-c But tell me what happens when it stops? <3 (Britney Spears) ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
On Mon, 10 May 2021 15:51:38 +0200 Fons Adriaensen wrote: >On Mon, May 10, 2021 at 03:17:12PM +0200, Jeanette C. wrote: > >> There are numerous audiofiles around containing one single cycle wave to >> be used with multiple wavetable synthesizers, both in hard and software. >> I can only assume that these are matched to the number of samples they >> contain. Some of them CERTAINLY proclaimed this fact, > >OK. In that case the actual frequency will be R / N where R is the sample >rate and N the length in samples. This is very probably not exactly an >musical pitch in the equally tempered scale, but that doesn't matter >since the wavetable synth will have to resample it anyway. > >So in this case, all you need is an FFT with a size equal to the lenght >of your single cycle sample. There is no faster method. > >I just checked, FFTW3 can do any size. > >Normally you'd just use the real-to-complex fft. For prime lengths, this >may become slower than normal (N^2 complexity instead of N log N). If this >matters (it probably won't), you could use the complex-to-complex fft with >the imaginary part set to zero, this will be faster (always N log N). > >In all cases, the N / 2 + 1 first elements of the output will correspond >to the harmonics, so you just the square root of the power of each. > >Ciao, > Not entirely clear on what you are doing, but you may find PadSynth the be a better option. It already creates perfectly looping samples (not sure if you can fiddle that for a single cycle) and will then export these over as a number of different pitches/key numbers. -- Will J Godfrey https://willgodfrey.bandcamp.com/ http://yoshimi.github.io Say you have a poem and I have a tune. Exchange them and we can both have a poem, a tune, and a song. ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
On Mon, May 10, 2021 at 03:17:12PM +0200, Jeanette C. wrote: > There are numerous audiofiles around containing one single cycle wave to > be used with multiple wavetable synthesizers, both in hard and software. > I can only assume that these are matched to the number of samples they > contain. Some of them CERTAINLY proclaimed this fact, OK. In that case the actual frequency will be R / N where R is the sample rate and N the length in samples. This is very probably not exactly an musical pitch in the equally tempered scale, but that doesn't matter since the wavetable synth will have to resample it anyway. So in this case, all you need is an FFT with a size equal to the lenght of your single cycle sample. There is no faster method. I just checked, FFTW3 can do any size. Normally you'd just use the real-to-complex fft. For prime lengths, this may become slower than normal (N^2 complexity instead of N log N). If this matters (it probably won't), you could use the complex-to-complex fft with the imaginary part set to zero, this will be faster (always N log N). In all cases, the N / 2 + 1 first elements of the output will correspond to the harmonics, so you just the square root of the power of each. Ciao, -- FA ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
May 10 2021, Fons Adriaensen has written: ... Maybe you could tell us what exactly you want to use this for, then a pragmatic solution could be found. There are numerous audiofiles around containing one single cycle wave to be used with multiple wavetable synthesizers, both in hard and software. I can only assume that these are matched to the number of samples they contain. Some of them CERTAINLY proclaimed this fact, AFAIK. I don't have a wavetable synth that would directly accept waves, but I have access to synthesizers with additive capabilities, including Yoshimi. So converting such "prepared" waveforms to additive spectra is quite useful. I already have a more general solution in Csound, which works with a generic FFT, to be used with real world samples. But it takes some time and contains its own hazards. :) So a dedicated solution seemed preferrable. Many thanks for your clear description and the useful caveats so far! Best wishes, Jeanette -- * Website: http://juliencoder.de - for summer is a state of sound * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g * Audiobombs: https://www.audiobombs.com/users/jeanette_c * GitHub: https://github.com/jeanette-c She's so lucky, she's a star But she cry, cry, cries in her lonely heart... <3 (Britney Spears) ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
Re: [LAD] Converting single-cycle wave to harmonic spectrum
On Mon, May 10, 2021 at 12:20:56PM +0200, Jeanette C. wrote: > The input waveform is exactly one single cycle > The waveform has only harmonic overtones. You may be making a dangerous assumption (if you want an accurate result): that a single cycle corresponds exactly to an integer number of samples. If that is the case, all you need is an FFT size equal to the number of samples, AFAIK, fftw3 can do any size. Then each bin of the FFT output will correspond exactly to an harmonic. But in general, a single cycle will not be an integer number of samples, and in that case there is no simple solution. Suppose you have N samples, but the actaul period is N + eps, with 0 < eps < 1. If you know eps, you could resample the signal to an integer number of samples or to some preferred FFT size. But now we have a new problem: in order to do the resampling you'll need some extra samples either before or after the ones you started with. If these are available, and if you know that they are valid (i.e. they represent an identical cycle), that is probably the way to go. And if not, there is no 'exact' solution. Maybe you could tell us what exactly you want to use this for, then a pragmatic solution could be found. Ciao, -- FA ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev
[LAD] Converting single-cycle wave to harmonic spectrum
Hey hey, I'm trying to convert a single cycle wave to a harmonic spectrum. I know that in theory this is a job for standard FFT. I wondered if there isn't a "simpler" way given a few assumptions about the input. The input waveform is exactly one single cycle The waveform has only harmonic overtones. That means that only integral multiples of the base frequency need to be considered and the specific Nyquist frequency is determined by the number of samples (I think). I treid searching, but it appears that I don't have the right keyword to hand, getting only very general results bordering on the off-topic. :) Can anyone help? A good keyword, name of an algorithm or a general name for this specific task would be helpful enough, I suppose. Best wishes, Jeanette -- * Website: http://juliencoder.de - for summer is a state of sound * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g * Audiobombs: https://www.audiobombs.com/users/jeanette_c * GitHub: https://github.com/jeanette-c She's so lucky, she's a star But she cry, cry, cries in her lonely heart... <3 (Britney Spears) ___ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev