r/DSP•
oadk

A few questions about the STFT

I've been trying to make my own audio pitch shifter in Octave (partially because I don't have Matlab, but I've been using it for so long I prefer it in ways). I have a decent understanding of Fourier transforms and there are multiple ways that I've been able to achieve the desired pitch shift - but with a recurring problem: I get a periodic noise that I think is associated to the transitions between my ~50ms sections that I split the audio signal into. I've tried a lot of things to reduce the noise, matching the amplitude of the sections at the transition, matching certain parts of the phase. I've improved it somewhat, but it's still a long way off what I can get other programs to do for me.

I've learnt a lot trying to do it myself, and I think it's good to give these things a shot your own way first, but now I'm at the point where I just want to find out how other people do it. This is when I came across the STFT, which seems to be a popular way to implement something known as a phase vocoder. While I thought I understood the rough concept of what the STFT achieved, I'm in particular having trouble figuring out how to use it in Octave.

It requires parameters for window size, increment (also referred to as hop) and the number of coefficients. Increment is the easy one, pretty clear that it's the increment applied to the index to reach the next segment. I'm assuming the number of coefficients is just the size of the FFT, similar to how I was using 50ms sections before (so 0.05 times sampling frequency). The thing I don't really get about the option for the window size is how it can be different to the size of the FFT. My impression was that if you have 1024 elements in your FFT then the window function would be stretched across those same 1024 elements.

I've played around with the function and looked at the size of the output matrix, but it hasn't really helped me understand it. If I do stft(y, 1024, 256, 1024) then the size of the output matrix is 2048,5. I thought maybe the window function was actually only the hanning (the default function) at the edges but unity for the middle 1024 elemets, 512 + 1024 + 512 = 2048. So I tried stft(y, 256, 256, 1024) which gave a matrix of size 2048,8. I've used lots of values and the output always seems to be twice my number of coefficients, and I don't understand why.

Sorry that the post was so long. I'm sure my misconceptions are many but it may be hard to pick them out of that wall of text, so does anyone know of a good diagram that shows how it fits together? I've found some with FFT size and increment/hop size, but I haven't seen any that show how the window functions fit into all of this.

Posted Dec 11, 2011, 7:18 PM UTC

79
cinnedy•

X = fft(x)

This command will take a time signal x N samples in length and transform it into a frequency representation, also N samples in length.

X = fft(x,M)

Does the same as the previous, but this time outputs an X M samples in length, by truncating or adding zeros to x. When M is bigger than N it is often useful for frequency domain interpolation. I would guess that this is the function of the number of coefficients. Plot your results: when you ask for a larger number of coefficients, has the spectrum been interpolated?

Concerning the phase vocoder, it's really very good for pitch shifting, especially if you want to work with audio signals generally. Check out: http://labrosa.ee.columbia.edu/matlab/pvoc/

However, if you want to pitch shift speech, you'd probably get better results with TDPSOLA, http://en.m.wikipedia.org/wiki/PSOLA

Good luck!

2
oadkOP•

Thanks for the reply. I didn't realise you could do frequency domain interpolation like that, but it makes sense. I was looking at the spectrum before, but I've started taking inverse FFTs of the output of the STFT and I think you're probably right about the interpolation. Here's the output for the inverse transform of stft(x, 1024, 256, 1024) where x is just a cosine:

http://i.imgur.com/5RP0N.png

It's pretty clearly 1024 elements of data with 1024 zeros added to the end of it. The odd thing is that if I do stft(x, 1024, 256, 512) then I get just the first 1024 elements without the zeros. This seems odd to me because it's exactly as I was expecting, but with an FFT size that is always double the number I use for "number of coefficients". Hopefully someone knows a reason why I'm getting this, but I think I can progress with how it's working in the meantime.

1
gdtchden1gma•

Are you inverting the STFT yourself, or did the STFT function come with an inverse?

1
rlbond86•

This is just my guess, because MATLAB does not seem to have a function called stft, or any short-time Fourier transform, though I have implemented one myself.

Keep in mind that you can use a varying number of samples when doing the fourier transform, though for a time-domain signal of length N, you need at least N frequency samples to reconstruct the signal perfectly. You are, however, allowed to oversample your signal. For example,

>> x = [-1 1 -2 3]
x =
    -1     1    -2     3

>> fft(x)
ans =
   1.0000             1.0000 + 2.0000i  -7.0000             1.0000 - 2.0000i

>> fft(x, 6)

ans =
   1.0000            -2.5000 + 0.8660i   2.5000 - 2.5981i  -7.0000             2.5000 + 2.5981i      -2.5000 - 0.8660i

So my guess is # samples refers to this.

1
qyatixcix•

Yes, using a larger fft size than window size is an oversampling. Important to note though that it doesn't "add information", it constitutes an ideal interpolation of the spectrum.

The window function is typically applied to the time-domain segment prior to the fourier transform. The properties of the window function affect the observation of the signal in a number of ways; some windows give a better dynamic range, others a better resolution. The paper that always gets cited for a list of these properties is:

Fredric J. Harris, "On the use of windows for harmonic analysis with discrete Fourier transform," Proceedings of the IEEE, 66(1), 1978 pp. 51-83.

Hann window is a fairly "moderate" choice, i.e. good for a number of applications.

edit: oh yeah about the size of the fourier transform an important point is that it should be a power of 2 to take advantage of the "fastness" of the fft. That's why these 1024, 2048, etc. "magic numbers" pop up everywhere. So if the window size is being adapted to the signal, the fft size will typically be rounded to the next power of two. It's been a while since I used matlab/octave but I think there is even a built in function for this : nextpow2 (or something).

3
rlbond86•

Yeah, Hann window is usually my go-to choice, it has good sidelobe reduction while keeping relatively good mainlobe resolution.

2
gdtchden1gma•

Applying a 'standard' window to the data before taking an FFT almost always degrades the resolution (increased mainlobe width) but improves the sidelobe structure (increases stopband depth). There a number of built-in windows that MATLAB provides including Hann, Hamming and Blackman windows.

In response to rlbond86, MATLAB does indeed have a built-in STFT function spectrogram http://www.mathworks.com/help/toolbox/signal/ref/spectrogram.html

The number of samples you use in taking the FFT controls the spectral resolution by increasing or decreasing the number of samples used for the FFT by either zero-padding or truncating the input.

Radix-2 transforms have particularly efficient implementations that increase computational speed, so using something like 2^nextpow2(Nfft) where Nfft is the nominal number of samples you'd like to use should speed up the calculations, even though (2^nextpow2(Nfft) ) > Nfft.

The STFT can be used as an overcomplete transform - in which case your output will be longer (have more samples than) your input. This depends on the window size and overlap used to calculate it, if you chose parameters that cause any overlap, the STFT of the data will be longer than the data itself. Similarly if you chose parameters that leave large blocks of data unused, the STFT will be shorter than the input data. If you're looking to maintain the same number of samples on both ends, you should chose a window size and lag that divide evenly e.g. for a 1024-length signal use a window 256 samples with a lag (or 'hop') of 256 samples. This will result in an output of size Nfft x 4, where Nfft is the number of FFT samples you use.

2
jewdai•

there is a function in Matlab for STFT its really screwy though. Its called spectrogram ()

2
oadkOP•

I think you were right about what it's actually calculating, but I still can't understand why it always chooses to use an FFT size twice that of the one I requested with the "number of coefficients" parameter.

1