| Philipp Le | a532cf6 | 2022-05-02 22:43:19 +0200 | [diff] [blame^] | 1 | # 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 | |
| 7 | import numpy as np |
| 8 | |
| 9 | |
| 10 | def 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 | |
| 19 | def 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)) |