tj80
Computer
- Nov 24, 2002
- 3
Hi all,
I'm trying to go about the task of samping a small piece of an audio signal and then reproducing that piece an arbitrary amount of times (repeating the same audio over and over I suppose you could say). I'm working in C++.
The way I'm going about doing it is like so (using an example):
1. I have a sinusoidal signal with a period of 765.62Hz (a tricky frequency), sampled at 32000Hz. I grab approx. 33 ms of it, which corresponds to approx. 2^10 or 1024 samples. I perform a 1024 point FFT of it. This gives me 1024 real values which make an even spectrum and 1024 imaginary values which make an odd spectrum (this is consistent with fourier transform properties).
2. I then go through each sample in the fft and perform a phase shifting of that sample. I'm following the theory that a time shift of m samples in the time domain corresponds to a phase shift of exp^-j*w*m in the frequency domain. So my equations are like this:
3. Next, I do an ifft of my phase shifted fft signal, and then stick the resulting 1024 sample time domain signal next to the original 1024 samples.
The problem I am having is that, for some strange reason, the phase of the signal is always perfect but the amplitudes are always flipped. For example, if the original sinusoid sample goes up-down-up-down, then the phase-shifted signal will always go down-up-down-up. And I can't figure out why!
The problem is not my fft or ifft, because I have done a straight fft and ifft of a signal and always got the original signal back. It most likely has something to do with my phase shifting calculations and also the fact that the period of the sinusoid is quite tricky to work with.
Any ideas? Thanks in advance.
I'm trying to go about the task of samping a small piece of an audio signal and then reproducing that piece an arbitrary amount of times (repeating the same audio over and over I suppose you could say). I'm working in C++.
The way I'm going about doing it is like so (using an example):
1. I have a sinusoidal signal with a period of 765.62Hz (a tricky frequency), sampled at 32000Hz. I grab approx. 33 ms of it, which corresponds to approx. 2^10 or 1024 samples. I perform a 1024 point FFT of it. This gives me 1024 real values which make an even spectrum and 1024 imaginary values which make an odd spectrum (this is consistent with fourier transform properties).
2. I then go through each sample in the fft and perform a phase shifting of that sample. I'm following the theory that a time shift of m samples in the time domain corresponds to a phase shift of exp^-j*w*m in the frequency domain. So my equations are like this:
Code:
(remember A*exp^j*w*m = A*cos(w*m) + A*j*sin(w*m))
for (i = 0; i < 1024; i++) { // 1024 samples in the fft
frequency = (i* sampleRate) / 1024
totalTimeShift = timeShift / sampleRate (for reproducing the original signal just once the timeShift in this case is 1024 samples)
phaseShiftAngle = 2 * pi * frequency * totalTimeShift
realValue[i] = (realValue[i] * cos(phaseShiftAngle)) + (imaginaryValue[i] * sin(phaseShiftAngle))
imaginaryValue[i] = (imaginaryValue[i] * cos(phaseShiftAngle)) - (realValue[i] * sin(phaseShiftAngle))
}
The problem I am having is that, for some strange reason, the phase of the signal is always perfect but the amplitudes are always flipped. For example, if the original sinusoid sample goes up-down-up-down, then the phase-shifted signal will always go down-up-down-up. And I can't figure out why!
The problem is not my fft or ifft, because I have done a straight fft and ifft of a signal and always got the original signal back. It most likely has something to do with my phase shifting calculations and also the fact that the period of the sinusoid is quite tricky to work with.
Any ideas? Thanks in advance.