mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:32:44 +00:00 
			
		
		
		
	 cd21e03225
			
		
	
	
		cd21e03225
		
	
	
	
	
		
			
			Calculating sin and cos at once is quite a bit cheaper than calculating them individually. x87 has even a dedicated instruction for it: `fsincos`.
		
			
				
	
	
		
			50 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/Complex.h>
 | |
| #include <AK/Math.h>
 | |
| #include <AK/Span.h>
 | |
| 
 | |
| namespace LibDSP {
 | |
| 
 | |
| constexpr void fft(Span<Complex<double>> sample_data, bool invert = false)
 | |
| {
 | |
|     int n = sample_data.size();
 | |
| 
 | |
|     for (int i = 1, j = 0; i < n; i++) {
 | |
|         int bit = n >> 1;
 | |
|         for (; j & bit; bit >>= 1)
 | |
|             j ^= bit;
 | |
|         j ^= bit;
 | |
| 
 | |
|         if (i < j)
 | |
|             swap(sample_data[i], sample_data[j]);
 | |
|     }
 | |
| 
 | |
|     for (int len = 2; len <= n; len <<= 1) {
 | |
|         double ang = 2 * AK::Pi<double> / len * (invert ? -1 : 1);
 | |
|         Complex<double> wlen = Complex<double>::from_polar(1., ang);
 | |
|         for (int i = 0; i < n; i += len) {
 | |
|             Complex<double> w = { 1., 0. };
 | |
|             for (int j = 0; j < len / 2; j++) {
 | |
|                 Complex<double> u = sample_data[i + j];
 | |
|                 Complex<double> v = sample_data[i + j + len / 2] * w;
 | |
|                 sample_data[i + j] = u + v;
 | |
|                 sample_data[i + j + len / 2] = u - v;
 | |
|                 w *= wlen;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     if (invert) {
 | |
|         for (int i = 0; i < n; i++)
 | |
|             sample_data[i] /= n;
 | |
|     }
 | |
| }
 | |
| 
 | |
| }
 |