mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-13 08:48:25 +00:00
[NumCS] Update/Add code examples
This commit is contained in:
@@ -8,21 +8,49 @@ Ein trigonometrisches Polynom $p_{N - 1}(t)$ kann effizient an den äquidistante
|
||||
Dazu muss das Polynom $p_{N - 1} \in \mathcal{T}_N \subseteq \mathcal{T}_M$ in der trigonometrischen Basis $\mathcal{T}_M$ neugeschrieben werden,
|
||||
in dem man \bi{Zero-Padding} verwendet, also Nullen im Koeffizientenvektor an den Stellen höheren Frequenzen einfügt.
|
||||
|
||||
\TODO Insert cleaned up code from Page 95 (part of exercises)
|
||||
|
||||
Die folgende Funktion wird im Script \texttt{evaliptrig} genannt.
|
||||
\innumpy Dieses Verfahren lässt sich in Python leicht via \verb|slices| umsetzen:
|
||||
\rmvspace
|
||||
\begin{code}{python}
|
||||
def evaluate_trigonometric_interpolation_polynomial(y: np.ndarray, N: int):
|
||||
n = len(y)
|
||||
if (n % 2) == 0:
|
||||
c = np.fft.ifft(y) # Fourier coefficients
|
||||
a = np.zeros(N, dtype=complex)
|
||||
def zero_pad(v: np.ndarray, N: int):
|
||||
"""Apply zero-padding to size N to a vector v """
|
||||
n = v.size
|
||||
if (N < n): raise ValueError(f"ERROR: Zeropadding for N smaller than vector length: {N} < {n}")
|
||||
|
||||
u = np.zeros(N, dtype=complex)
|
||||
u[:n//2] = v[:n//2]
|
||||
u[N-n//2:] = v[n//2:]
|
||||
return u
|
||||
|
||||
# Zero padding
|
||||
a[: n // 2] = c[: n // 2]
|
||||
a[N - n // 2 :] = c[n // 2 :]
|
||||
return np.fft.fft(a)
|
||||
else:
|
||||
raise ValueError("odd length")
|
||||
|
||||
def eval_trig_poly(y: np.ndarray, N: int):
|
||||
""" Evaluate trig poly generated using y on N points """
|
||||
n = y.size
|
||||
if (n % 2 != 0): raise ValueError(f"ERROR: y must be of even length, len(y)={n}")
|
||||
|
||||
coeffs = np.fft.fft(y) * 1/n
|
||||
coeffs = zero_pad(coeffs, N)
|
||||
return np.fft.ifft(coeffs) * N
|
||||
\end{code}
|
||||
|
||||
\subsubsection{Ableitungen mit Zero-Padding}
|
||||
Mit dem Trick aus Bemerkung 3.2.13 lassen sich auch direkt die Ableitungen berechnen.
|
||||
|
||||
\innumpy Geht dies direkt durch leichte Modifikation der obigen Funktionen:
|
||||
|
||||
\begin{code}{python}
|
||||
def eval_trig_poly_d1(y: np.ndarray, N: int):
|
||||
""" Evaluates first der. of trig poly generated using y on N points """
|
||||
n = y.size
|
||||
if (n % 2 != 0): raise ValueError(f"ERROR: y must be of even length, len(y)={n}")
|
||||
|
||||
coeffs = np.fft.fft(y) * 1/n
|
||||
|
||||
for i in range(0, n//2):
|
||||
coeffs[i] *= (2.0j * np.pi * i)
|
||||
for i in range(n//2, n):
|
||||
coeffs[i] *= (2.0j * np.pi * (i - n))
|
||||
|
||||
coeffs = zero_pad(coeffs, N)
|
||||
return np.fft.ifft(coeffs) * N
|
||||
\end{code}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user