flat assembler
Message board for the users of flat assembler.

Index > Main > Fast Fourier sse for wav sound? I looking for example

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1807
Roman 14 Sep 2024, 13:33
I looking example.
Fast Fourier transform – FFT
Post 14 Sep 2024, 13:33
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1807
Roman 15 Sep 2024, 09:02
I found this
Code:
void Fft2(double *in_Buffer, double *out_Buffer, long fftFrameSize, long sign)
{
    double wr, wi, arg, *p1, *p2, temp;
    double tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
    long i, bitm, j, le, le2, k;
 
    memcpy(out_Buffer, in_Buffer, fftFrameSize*2*sizeof(double));
 
    for (i = 2; i < 2*fftFrameSize-2; i += 2)
    {
        for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1)
        {
            if (i & bitm) j++;
            j <<= 1;
        }
        if (i < j)
        {
            p1 = out_Buffer+i; p2 = out_Buffer+j;
            temp = *p1; *(p1++) = *p2;
            *(p2++) = temp; temp = *p1;
            *p1 = *p2; *p2 = temp;
        }
    }
    long lim = (long)(log((double)fftFrameSize)/log(2.)+.5);
    for (k = 0, le = 2; k < lim; k++)
    {
        le <<= 1;
        le2 = le>>1;
        ur = 1.0;
        ui = 0.0;
        arg = pi / (le2>>1);
        wr = cos(arg);
        wi = sign*sin(arg);
        for (j = 0; j < le2; j += 2)
        {
            p1r = out_Buffer+j; p1i = p1r+1;
            p2r = p1r+le2; p2i = p2r+1;
            for (i = j; i < 2*fftFrameSize; i += le)
            {
                tr = *p2r * ur - *p2i * ui;
                ti = *p2r * ui + *p2i * ur;
                *p2r = *p1r - tr; *p2i = *p1i - ti;
                *p1r += tr; *p1i += ti;
                p1r += le; p1i += le;
                p2r += le; p2i += le;
            }
            tr = ur*wr - ui*wi;
            ui = ur*wi + ui*wr;
            ur = tr;
        }
    }
}
    


Last edited by Roman on 15 Sep 2024, 09:05; edited 1 time in total
Post 15 Sep 2024, 09:02
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1807
Roman 15 Sep 2024, 09:03
write to fasm
Code:
macro ExitMsg a { invoke MessageBox,0,a,'ExitToWin',0
      invoke  ExitProcess,0      }

section '.code' code readable writeable executable 
        align 16
         
        p2       dd 1.0,0,0,0 
        p3       dd 2.0,0,0,0 
        point    dd 8.0,0,0,0 

Start:

        push 2          ;size_data
        push xmDbgTxt   ;out_data
        push p2         ;in_data
        call FFT_SSE2
        ExitMsg 'exit'


in_data         equ     dword   [ebp+8]
out_data        equ     dword   [ebp+12]
size_data       equ     dword   [ebp+16]
 
; local
dtemp   equ     dword   [ebp-4]       ;dd     0
ftemp   equ     qword   [ebp-12]      ;dq     0.0
wr      equ     qword   [ebp-20]      ;dq     0.0
wi      equ     qword   [ebp-28]      ;dq     0.0
FFT_SSE2:
push    ebp
mov     ebp, esp
sub     esp, 28
push    ebx
push    esi
push    edi
 
; memcpy(out_Buffer, in_Buffer, fftFrameSize*2*sizeof(double));
 
mov     ecx, size_data
shl     ecx, 2                          ; * 2 * sizeof(double)
mov     esi, in_data
mov     edi, out_data
rep     movsd
 
mov     ecx, 2                          ; ecx - i
_l0:
 
mov     edx, 2                          ; edx - bitm
xor     ebx, ebx                        ; ebx - j
_l1:
 
mov     eax, ecx
and     eax, edx
jz      _l3
inc     ebx
_l3:
shl     ebx, 1
 
shl     edx, 1
mov     eax, size_data
shl     eax, 1
cmp     edx, eax
jb      _l1
 
cmp     ecx, ebx
jnb     _l4
mov     eax, out_data
movapd  xmm0, dqword  [eax+ecx*8]     ; p1 = out_Buffer+i
movapd  xmm1, dqword   [eax+ebx*8]     ; p2 = out_Buffer+j
movapd  dqword   [eax+ecx*8], xmm1
movapd  dqword   [eax+ebx*8], xmm0
_l4:
 
add     ecx, 2
mov     eax, size_data
shl     eax, 1
sub     eax, 2
cmp     ecx, eax
jb      _l0
 
fld1
fild    size_data
fyl2x
frndint
fistp   dtemp                           ; (long)log2(size)
mov     edx, dtemp
 
xor     ecx, ecx                                ; ecx - k
mov     ebx, 2                          ; ebx - le
main_loop:
 
push    edx
mov     eax, ebx
shl     ebx, 1
fldpi
shr     eax, 1
mov     dtemp, eax
fidiv   dtemp                           ; st(0): arg = pi / (le2>>1)
fsincos                                 ; st(0)=cos(arg) st(1)=sin(arg)
fstp    wr
fchs
fstp    wi
xorpd   xmm2, xmm2                      ; ur|ui
fld1
fstp    ftemp
movlps  xmm2,  ftemp
 
xor     esi, esi                        ; esi - j
_loop1:
 
mov     eax, out_data
mov     edx, esi
shl     edx, 3
add     eax, edx                        ; eax = out_data + j
 
mov     edi, esi
_loop2:
 
movapd  xmm3, xmm2      ; push xmm2
 
movapd  xmm0, xmm2
movddup xmm1, qword   [eax+ebx*4+8]
movddup xmm2, qword   [eax+ebx*4]
mulpd   xmm2, xmm0
shufpd  xmm0, xmm0, 1
mulpd   xmm1, xmm0
addsubpd xmm2, xmm1
 
movapd  xmm0, dqword   [eax]         ; *p1
subpd   xmm0, xmm2
movapd  dqword   [eax+ebx*4], xmm0
 
movapd  xmm0, dqword   [eax]
addpd   xmm0, xmm2
movapd  dqword   [eax], xmm0
 
movapd  xmm2, xmm3      ; pop xmm2
 
push    ebx
shl     ebx, 3
add     eax, ebx
pop     ebx
 
add     edi, ebx
mov     edx, size_data
shl     edx, 1
cmp     edi, edx
jb      _loop2
 
movapd  xmm0, xmm2
movddup xmm1,  wi
movddup xmm2, wr
mulpd   xmm2, xmm0
shufpd  xmm0, xmm0, 1
mulpd   xmm1, xmm0
addsubpd xmm2, xmm1
 
add     esi, 2
mov     eax, ebx
shr     eax, 1
cmp     esi, eax
jb      _loop1
 
inc     ecx
pop     edx
cmp     ecx, edx
jb      main_loop
 
pop edi
pop esi
pop ebx
mov     esp, ebp
pop     ebp
ret

SECTION '.idata' IMPORT DATA READABLE WRITEABLE

     library  kernel32, 'KERNEL32.DLL',\
              user32,   'USER32.DLL',\
              winmm,    'WINMM.DLL',\
              msvcrt,   'MSVCRT.DLL'


 include 'fasmAPI\kernel32.inc'
 include 'fasmAPI\user32.inc'
 include 'fasmAPI\winmm.inc'
 include 'fasmAPI\msvcrt.inc'

section '.bss' readable writeable
        align 16
        xmDbgTxt        rd      8000     
    
Post 15 Sep 2024, 09:03
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.