# How to generate a sinusoid with STFT at a frequency which is between bins

Is it possible to generate a sinusoid at a frequency which is between two bins (so a fractional index) with the STFT algorithm (by filling bins and doing the inverse) ? If yes then how can this be done ?

In my experiment with overlap add STFT algorithm and with a window size of 1024 i just compute the distribution of energy between the two bins that the sinusoid fall into however this produce two sinusoids plus some noise in between... is that normal ?

• Why do you want to use the (inverse?) STFT, instead of just plain IFFT? – MBaz Mar 20 at 22:52
• Because i want to do it in realtime, i also want to generate signals and mix it with signal transformations. – Onirom Mar 20 at 23:17

A sinusoid with a frequency that is between bins in the FFT frequency domain is circularly discontinuous in the time domain. So you can't use the same IFFT results back-to-back without the noise from this discontinuity between each IFFT window, as the end of one window will have a value too far from the beginning of the next window (unless your frequency is bin centered, e.g. exactly integer periodic in the FFT's length).

If you calculate and vary the starting phase for each successive (complex input) IFFT, then you might be able to reduce or get rid of noise from the window discontinuity.

Note that the spectrum of a "between bin" frequency sinusoid is not just a cross-fade between the two nearest bins, but a Sinc shaped spectrum, spanning the full width of the FFT. (Or a windowed Sinc function for a decent approximation.)

If yes then how can this be done ?

Yes, it can be done. But doing it directly in the frequency domain is awkward, cumbersome and slow.

The most efficient way is to generate it directly in the time domain using a rotating phasor. If you need it in the STFT domain, just generate a properly aligned frame of data in the time domain, window and FFT it. That's going to be a lot quicker than doing directly in the STFT domain.

• i'm generally in agreement with Hilmar on this. but it is possible to lay down exactly the Fourier transform of a single windowed sinusoid. if it's a Hann window, the peak corresponding to the sinusoid will have sidelobes, but if it's a Gaussian window, the peak doesn't really have much sidelobes. and an analytical expression can be derived for the general peak. you can just add them into the spectrum and inverse DFT. – robert bristow-johnson Mar 21 at 23:56
• Of course it can be done this way. All I'm saying it's easier & quicker to do it in the time to domain :-) – Hilmar Mar 22 at 17:30

If this is about using the STFT, then it's about frames of audio (or whatever signal class) and, if it's about frames, it's about windows. Usually the windows we want are complementary, they add to 1. An example would be a Hann window.

So now imagine your sinusoid of an arbitrary frequency (mid bin or between bins or something else) being multiplied by a Hann window, then you DFT that. You will see a peak corresponding to something very near the frequency you started with. If you can construct that spectrum in the frequency domain and then inverse DFT, you will get that windowed sinusoid in the time domain.