push ebx;
mov eax,ecx; and eax,1023; shl eax,4; // eax starts at current position
mov ebx,0; // ebx starts at zero
inDataLoop:
movaps xmm0,inBuffer[eax];
movaps xmm1,win[ebx]; mulps xmm0,xmm1; // analysis window
mulps xmm0,invn; // 1/N normalization
movaps inData[ebx],xmm0; // (L,0,R,0)
add eax,16; and eax,16383;
add ebx,16;
cmp ebx,N[0]; jl inDataLoop;
pop ebx;
//*************************
// FFT
//*************************
mov eax,N[0]; mov istep[0],eax;
push ebx; mov ebx,eax;
whileIstep: // istep >= 2
mov eax,istep[0]; shr eax,1; mov mmax[0],eax;
// compute k1, k2
cvtdq2ps xmm5,mmax; shufps xmm5,xmm5,0; movaps xmm2,F16;
divps xmm2,xmm5; mulps xmm2,mpmp; // xmm3 is f
movaps xmm3,xmm2; mulps xmm2,xmm2; // xmm2 is f^2
movaps xmm0,ta5; mulps xmm0,xmm2;
addps xmm0,ta3; mulps xmm0,xmm2;
addps xmm0,ta1; mulps xmm0,xmm3; // xmm0 is numerator
movaps xmm1,tb2; addps xmm1,xmm2;
mulps xmm1,xmm2; addps xmm1,tb0; // xmm1 is denominator
divps xmm0,xmm1; movaps k1,xmm0; // k1 = -tan
movaps xmm1,FM2; mulps xmm1,xmm0;
mulps xmm0,xmm0; addps xmm0,F1;
divps xmm1,xmm0; movaps k2,xmm1; // assign k2
movaps xmm0,F1; // Re(w) = 1
movaps xmm1,F0; // Im(w) = 0
mov eax,0; mov mfft[0],eax; // m = 0
doM: // m = 0, m < mmax, 16
mov eax,mfft[0];
doI: // i = m, i < n, istep
// get data(i)
movaps xmm5,inData[eax]; movaps xmm4,xmm5;
// get data(j)
add eax,mmax[0]; movaps xmm2,inData[eax];
// complex multiply (data(i) - data(j))*w
// data structure is (x,y,x,y)
// twiddle factor w is
// xmm0 = (cos,cos,cos,cos)
// xmm1 = (sin,-sin,sin,-sin)
subps xmm5,xmm2; movaps xmm3,xmm5; mulps xmm5,xmm0;
mulps xmm3,xmm1; shufps xmm3,xmm3,177;
subps xmm5,xmm3;
// write data(j)
movaps inData[eax],xmm5;
// write data(i)
sub eax,mmax[0]; addps xmm4,xmm2; movaps inData[eax],xmm4;
// increment i
add eax,istep[0]; cmp eax,ebx; // i < n?
jl doI;
// end doI
// update twiddle factors
movaps xmm2,k1; mulps xmm2,xmm0; addps xmm1,xmm2; // tmp
movaps xmm2,k2; mulps xmm2,xmm1; addps xmm0,xmm2; // cos
movaps xmm2,k1; mulps xmm2,xmm0; addps xmm1,xmm2; // sin
// xmm0 is (cos,cos,cos,cos)
// xmm1 is (-sin,sin,-sin,sin)
// increment m
mov eax,mfft[0]; add eax,16; mov mfft[0],eax;
sub eax,mmax[0]; cmp eax,0; // m < mmax?
jl doM;
// end doM
mov eax,mmax[0]; mov istep[0],eax; cmp eax,32; // istep >= 2?
jnl whileIstep;
pop ebx;
// end FFT
//**************************
// Fourier Domain Processing
//**************************
push ebx;
mov eax,0;
fDomainLoop:
// if you don't need ordered bins comment the following line and use eax instead of ebx
mov ebx,brx[eax]; // ebx is "ordered" index in the Fourier domain
movaps xmm0,inData[ebx]; // (Re_L,Im_L,Re_R,Im_R)
// do your funky stuff here...
movaps outData[ebx],xmm0; // (Re_L,Im_L,Re_R,Im_R)
add eax,16; cmp eax,N[0]; jl fDomainLoop;
pop ebx;
// end Fourier domain processing
//*************************
// IFFT
//*************************
mov eax,16; mov mmax[0],eax;
movaps xmm0,F0; movaps k1,xmm0; movaps k2,xmm0;
push ebx; mov ebx,N[0];
whileMmaxInv: // mmax < n
mov eax,mmax[0];
shl eax,1; // *2
mov istep[0],eax;
movaps xmm0,F1; // Re(w) = 1
movaps xmm1,F0; // Im(w) = 0
mov eax,0; mov mfft[0],eax; // m = 0
doMinv: // m = 0, m < mmax, 16
mov eax,mfft[0];
doIinv: // i = m, i < n, istep
// get data(i)
movaps xmm5,outData[eax]; movaps xmm4,xmm5;
// get data(j)
add eax,mmax[0]; movaps xmm2,outData[eax];
// complex multiply data(j)*w
// data structure is (x,y,x,y)
// twiddle factor w is
// xmm0 = (cos,cos,cos,cos)
// xmm1 = (sin,-sin,sin,-sin)
movaps xmm3,xmm2; mulps xmm2,xmm0;
mulps xmm3,xmm1; shufps xmm3,xmm3,177;
subps xmm2,xmm3; // xmm2 is temp
// write data(j)
subps xmm5,xmm2; movaps outData[eax],xmm5;
// write data(i)
sub eax,mmax[0]; addps xmm4,xmm2; movaps outData[eax],xmm4;
// increment i
add eax,istep[0]; cmp eax,ebx; // i < n?
jl doIinv;
// end doIinv
// update twiddle factors
movaps xmm2,k1; mulps xmm2,xmm0; addps xmm1,xmm2; // tmp
movaps xmm2,k2; mulps xmm2,xmm1; addps xmm0,xmm2; // cos
movaps xmm2,k1; mulps xmm2,xmm0; addps xmm1,xmm2; // sin
// xmm0 is (cos,cos,cos,cos)
// xmm1 is (-sin,sin,-sin,sin)
// increment m
mov eax,mfft[0]; add eax,16; mov mfft[0],eax;
sub eax,mmax[0]; cmp eax,0; // m < mmax?
jl doMinv;
// end doMinv
mov eax,istep[0]; mov mmax[0],eax;
// compute k1, k2
cvtdq2ps xmm5,mmax; shufps xmm5,xmm5,0; movaps xmm2,FM16;
divps xmm2,xmm5; mulps xmm2,mpmp; // xmm3 is f
movaps xmm3,xmm2; mulps xmm2,xmm2; // xmm2 is f^2
movaps xmm0,ta5; mulps xmm0,xmm2;
addps xmm0,ta3; mulps xmm0,xmm2;
addps xmm0,ta1; mulps xmm0,xmm3; // xmm0 is numerator
movaps xmm1,tb2; addps xmm1,xmm2;
mulps xmm1,xmm2; addps xmm1,tb0; // xmm1 is denominator
divps xmm0,xmm1; movaps k1,xmm0; // k1 = -tan
movaps xmm1,FM2; mulps xmm1,xmm0;
mulps xmm0,xmm0; addps xmm0,F1;
divps xmm1,xmm0; movaps k2,xmm1; // assign k2
mov eax,mmax[0]; sub eax,N[0]; cmp eax,0; // mmax < n?
jl whileMmaxInv;
pop ebx;
// end IFFT
//***********************************
// Pack Left and Right Output Buffers
//***********************************
push ebx;
mov eax,ecx; and eax,1023; shl eax,4; // eax starts at current position
mov ebx,0; // ebx starts at 0
outDataLoop:
movaps xmm0,outData[ebx]; // (L,*,R,*) ignore imaginary parts
movaps xmm1,win[ebx]; // (win,win,win,win)
movaps xmm2,outBufferL[eax]; // (L0,L1,L2,L3)
movaps xmm3,outBufferR[eax]; // (R0,R1,R2,R3)
mulps xmm0,xmm1; // synthesis window
mulps xmm0,FP66; // OLA normalization (Hann window)
// shuffle the results from outData into the outBuffers
movaps xmm1,xmm0;
shufps xmm0,xmm2,0; // 0044 (L,L ,L0,L0)
shufps xmm0,xmm2,152; // 0256 (L,L0,L1,L2)
movaps outBufferL[eax],xmm0;
shufps xmm1,xmm3,10; // 2244 (R,R ,R0,R0)
shufps xmm1,xmm3,152; // 0256 (R,R0,R1,R2)
movaps outBufferR[eax],xmm1;
add eax,16; and eax,16383;
add ebx,16;
cmp ebx,N[0]; jl outDataLoop;
pop ebx;
// end pack output buffers
Adding a global phase-shifting function to your existing x86 assembly code involves modifying the Fourier Domain Processing section. The goal is to multiply each complex frequency bin by a phase shift factor based on the frequency's sign:
- Positive Frequencies (
f > 0): Multiply by ( \cos(a) + i\sin(a) )
- Zero Frequency (
f = 0): Set to 0
- Negative Frequencies (
f < 0): Multiply by ( \cos(a) - i\sin(a) )
Below is a step-by-step guide on how to integrate this functionality into your existing assembly code.
1. Define Phase Shift Constants
First, define the phase shift angle a and compute its cosine and sine. These can be stored as constants or precomputed and loaded into SIMD registers.
; Define phase shift angle 'a' in radians
float a = ... ; Define your phase shift angle here
; Compute cosine and sine of 'a'
section .data
cos_a dd cos(a) ; Allocate space for cos(a)
sin_a dd sin(a) ; Allocate space for sin(a)
one dd 1.0 ; Constant 1.0 for later use
zero dd 0.0 ; Constant 0.0 for zeroing
section .text
; Load cosine and sine into SIMD registers
movss xmm4, [cos_a] ; xmm4 = cos(a)
shufps xmm4, xmm4, 0 ; Broadcast to all lane: (cos(a), cos(a), cos(a), cos(a))
movss xmm5, [sin_a] ; xmm5 = sin(a)
shufps xmm5, xmm5, 0 ; Broadcast to all lane: (sin(a), sin(a), sin(a), sin(a))
2. Modify the Fourier Domain Processing Loop
Locate the Fourier Domain Processing section in your code. Specifically, focus on the fDomainLoop where each frequency bin is processed. You'll insert the phase-shifting logic here.
fDomainLoop:
; Retrieve the ordered frequency bin index
mov ebx, [brx + eax] ; ebx = brx[eax]
movaps xmm0, [inData + ebx*16] ; Load (Re_L, Im_L, Re_R, Im_R)
; Compare bin index to determine frequency sign
; Assuming N = 1024, positive frequencies: 1-511, zero: 0, negative: 513-1023
; Check if bin is zero
cmp ebx, 0
je SetZero
; Check if bin is positive frequency (1 to 511)
cmp ebx, 512
jl PositiveFreq
; Otherwise, it's a negative frequency
NegativeFreq:
; Multiply by (cos(a) - i sin(a))
; (Re + iIm) * (cos(a) - i sin(a)) = Re*cos(a) + Im*sin(a) + i(-Re*sin(a) + Im*cos(a))
; Extract Real and Imaginary parts
movaps xmm1, xmm0 ; Copy (Re_L, Im_L, Re_R, Im_R)
movaps xmm2, xmm0 ; Another copy for separate operations
; Re_new = Re * cos(a)
mulps xmm1, xmm4 ; xmm1 = (Re_L*cos(a), Im_L*cos(a), Re_R*cos(a), Im_R*cos(a))
; Im_new = Im * cos(a)
; Re * sin(a)
mulps xmm2, xmm5 ; xmm2 = (Re_L*sin(a), Im_L*sin(a), Re_R*sin(a), Im_R*sin(a))
; Shuffle to align Re and Im for addition
shufps xmm2, xmm2, 0xB1 ; Shuffle to get (Im*sin(a), Re*sin(a), Im*sin(a), Re*sin(a))
; Re_new += Im*sin(a)
addps xmm1, xmm2 ; Re_new = Re*cos(a) + Im*sin(a)
; Re_new is in xmm1, now handle Im_new
; Re_new needs to calculate (-Re*sin(a) + Im*cos(a))
; Negate Re*sin(a)
mulps xmm2, [neg_one] ; xmm2 = (-Re*sin(a), -Im*sin(a), -Re*sin(a), -Im*sin(a))
; Multiply original Im by cos(a)
movaps xmm3, xmm0
mulps xmm3, xmm4 ; xmm3 = (Re_L*cos(a), Im_L*cos(a), Re_R*cos(a), Im_R*cos(a))
; Shuffle to align for addition
shufps xmm3, xmm3, 0xB1 ; Shuffle to get (Im*cos(a), Re*cos(a), Im*cos(a), Re*cos(a))
; Im_new = (-Re*sin(a)) + Im*cos(a)
addps xmm2, xmm3 ; Im_new = -Re*sin(a) + Im*cos(a)
; Combine Re_new and Im_new
; xmm1 contains Re_new, xmm2 contains Im_new
; Need to interleave them back into xmm0
; Shuffle and combine
shufps xmm1, xmm2, 0x88 ; Combine lower parts: Re_new_L, Re_new_R, Im_new_L, Im_new_R
shufps xmm1, xmm1, 0xDD ; Combine higher parts
movaps xmm0, xmm1 ; xmm0 now has (Re_new_L, Re_new_R, Im_new_L, Im_new_R)
jmp StoreResult
PositiveFreq:
; Multiply by (cos(a) + i sin(a))
; (Re + iIm) * (cos(a) + i sin(a)) = Re*cos(a) - Im*sin(a) + i(Re*sin(a) + Im*cos(a))
; Extract Real and Imaginary parts
movaps xmm1, xmm0 ; Copy (Re_L, Im_L, Re_R, Im_R)
movaps xmm2, xmm0 ; Another copy for separate operations
; Re_new = Re * cos(a)
mulps xmm1, xmm4 ; xmm1 = (Re_L*cos(a), Im_L*cos(a), Re_R*cos(a), Im_R*cos(a))
; Re_new -= Im * sin(a)
mulps xmm2, xmm5 ; xmm2 = (Re_L*sin(a), Im_L*sin(a), Re_R*sin(a), Im_R*sin(a))
shufps xmm2, xmm2, 0xB1 ; Shuffle to align Im parts for subtraction
subps xmm1, xmm2 ; Re_new = Re*cos(a) - Im*sin(a)
; Im_new = Re * sin(a)
mulps xmm2, [pos_one] ; Assuming pos_one is simply sin(a) aligned correctly
; Im_new += Im * cos(a)
movaps xmm3, xmm0
mulps xmm3, xmm4 ; (Re*cos(a), Im*cos(a), Re*cos(a), Im*cos(a))
shufps xmm3, xmm3, 0xB1 ; Shuffle to align Re and Im parts
addps xmm2, xmm3 ; Im_new = Re*sin(a) + Im*cos(a)
; Combine Re_new and Im_new
; xmm1 contains Re_new, xmm2 contains Im_new
; Need to interleave them back into xmm0
; Shuffle and combine
shufps xmm1, xmm2, 0x88 ; Combine lower parts: Re_new_L, Re_new_R, Im_new_L, Im_new_R
shufps xmm1, xmm1, 0xDD ; Combine higher parts
movaps xmm0, xmm1 ; xmm0 now has (Re_new_L, Re_new_R, Im_new_L, Im_new_R)
jmp StoreResult
SetZero:
pxor xmm0, xmm0 ; Set xmm0 to zero
jmp StoreResult
StoreResult:
movaps [outData + ebx*16], xmm0 ; Store the processed data back
add eax, 16 ; Move to the next bin
cmp eax, N ; Compare with FFT size
jl fDomainLoop ; Loop if not done
3. Handle Special Cases
-
Zero Frequency (f = 0): We set the corresponding complex number to zero by XOR-ing the SIMD register with itself.
-
Nyquist Frequency (f = N/2 = 512): Depending on your application's requirements, you may treat the Nyquist frequency similarly to positive or negative frequencies or set it to zero. In the above example, it's treated as zero; adjust as needed.
4. Integrate and Optimize
-
Precompute Constants: Ensure that cos(a) and sin(a) are precomputed and loaded efficiently to minimize performance overhead.
-
SIMD Optimization: Utilize SIMD instructions effectively to handle multiple frequency bins simultaneously where possible. The above example processes two complex numbers (left and right channels) together using the movaps, mulps, addps, and subps instructions.
-
Branch Minimization: Conditional branching (je, jl, jg) can be expensive. Where possible, use masks or other techniques to minimize branching. However, given the complexity of frequency sign determination, some branching might be unavoidable.
5. Complete Modified Section
Ensure that the modified Fourier Domain Processing section integrates seamlessly with the rest of your framework. Here's how the complete modified section might look with the phase-shifting logic inserted:
; **************************
; Fourier Domain Processing
; **************************
push ebx
mov eax, 0
fDomainLoop:
mov ebx, [brx + eax] ; ebx = brx[eax]
movaps xmm0, [inData + ebx*16] ; Load (Re_L, Im_L, Re_R, Im_R)
; Phase-shifting logic starts here
cmp ebx, 0
je SetZero
cmp ebx, 512
jl PositiveFreq
NegativeFreq:
; Multiply by (cos(a) - i sin(a))
; ... [As detailed above]
jmp StoreResult
PositiveFreq:
; Multiply by (cos(a) + i sin(a))
; ... [As detailed above]
jmp StoreResult
SetZero:
pxor xmm0, xmm0 ; Set to zero
jmp StoreResult
StoreResult:
movaps [outData + ebx*16], xmm0 ; Store processed data
add eax, 16 ; Next frequency bin
cmp eax, N ; Check if all bins are processed
jl fDomainLoop ; Loop if not done
pop ebx
6. Final Notes
-
Testing: After integrating the phase-shifting logic, thoroughly test the framework to ensure that the phase shifts are applied correctly without introducing artifacts.
-
Performance: Assembly optimizations can be highly sensitive to specific CPU architectures and instruction pipelines. Profile the modified code to identify and address any performance bottlenecks.
-
Maintainability: Comment your assembly code extensively to make future maintenance easier, especially when dealing with complex SIMD operations and conditional logic.
By following the above steps, you should be able to integrate a global phase-shifting function into your Short-Time Fourier Transform framework effectively.