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
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!
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.
Are you inverting the STFT yourself, or did the STFT function come with an inverse?