blob: 4acde7fcfeb41f499e431ceedca730964c2941cd [file] [log] [blame]
Philipp Lea532cf62022-05-02 22:43:19 +02001# SPDX-License-Identifier: MPL-2.0
2# Copyright (c) 2022 Philipp Le <philipp@philipple.de>.
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7import numpy as np
8
9
10def swap_freq(x: np.ndarray, axis: int = 0) -> np.ndarray:
11 transposed = np.swapaxes(x, 0, axis)
12 swapped = np.zeros(transposed.shape, dtype=np.complex128)
13 length = transposed.shape[0]
14 swapped[int(length/2):, ] = transposed[:int(length/2), ]
15 swapped[:int(length/2), ] = transposed[int(length/2):, ]
16 return np.swapaxes(swapped, axis, 0)
17
18
19def abs_log_fft(x: np.ndarray, factor: float = 20, axis: int = 0) -> np.ndarray:
20 return factor * np.log10(np.abs(swap_freq(np.fft.fft(x), axis)) / len(x))