मैं निम्नलिखित सूत्र के अनुसार फूरियर श्रृंखला समारोह को लागू करने की कोशिश की गणना करें:त्रिकोणमिति दृष्टिकोण के साथ फूरियर श्रृंखला
... जहां ...
... और ...
यहाँ समस्या के लिए मेरे दृष्टिकोण है:
import numpy as np
import pylab as py
# Define "x" range.
x = np.linspace(0, 10, 1000)
# Define "T", i.e functions' period.
T = 2
L = T/2
# "f(x)" function definition.
def f(x):
return np.sin(np.pi * 1000 * x)
# "a" coefficient calculation.
def a(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a)/accuracy
integration = 0
for i in np.linspace(a, b, accuracy):
x = a + i * dx
integration += f(x) * np.cos((n * np.pi * x)/L)
integration *= dx
return (1/L) * integration
# "b" coefficient calculation.
def b(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a)/accuracy
integration = 0
for i in np.linspace(a, b, accuracy):
x = a + i * dx
integration += f(x) * np.sin((n * np.pi * x)/L)
integration *= dx
return (1/L) * integration
# Fourier series.
def Sf(x, L, n = 10):
a0 = a(0, L)
sum = 0
for i in np.arange(1, n + 1):
sum += ((a(i, L) * np.cos(n * np.pi * x)) + (b(i, L) * np.sin(n * np.pi * x)))
return (a0/2) + sum
# x axis.
py.plot(x, np.zeros(np.size(x)), color = 'black')
# y axis.
py.plot(np.zeros(np.size(x)), x, color = 'black')
# Original signal.
py.plot(x, f(x), linewidth = 1.5, label = 'Signal')
# Approximation signal (Fourier series coefficients).
py.plot(x, Sf(x, L), color = 'red', linewidth = 1.5, label = 'Fourier series')
# Specify x and y axes limits.
py.xlim([0, 10])
py.ylim([-2, 2])
py.legend(loc = 'upper right', fontsize = '10')
py.show()
... और यहाँ परिणाम की साजिश रचने के बाद मैं क्या मिलता है:
मैं पढ़ा है How to calculate a Fourier series in Numpy? और मैंने पहले से ही इस दृष्टिकोण को लागू किया है। यह बहुत अच्छा काम करता है, लेकिन यह एक्सपोटेंशियल विधि का उपयोग करता है, जहां मैं a_{n}
और b_{n}
गुणांक के अभिन्न अंगों की गणना के मामले में त्रिकोणमिति कार्यों और आयताकार विधि पर ध्यान केंद्रित करना चाहता हूं।
अग्रिम धन्यवाद।
अद्यतन (हल)
अंत में, यहाँ कोड का एक काम कर उदाहरण है। हालांकि, मैं इस पर अधिक समय बिताऊंगा, इसलिए अगर कुछ भी सुधार किया जा सकता है, तो यह किया जाएगा।
from __future__ import division
import numpy as np
import pylab as py
# Define "x" range.
x = np.linspace(0, 10, 1000)
# Define "T", i.e functions' period.
T = 2
L = T/2
# "f(x)" function definition.
def f(x):
return np.sin((np.pi) * x) + np.sin((2 * np.pi) * x) + np.sin((5 * np.pi) * x)
# "a" coefficient calculation.
def a(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a)/accuracy
integration = 0
for x in np.linspace(a, b, accuracy):
integration += f(x) * np.cos((n * np.pi * x)/L)
integration *= dx
return (1/L) * integration
# "b" coefficient calculation.
def b(n, L, accuracy = 1000):
a, b = -L, L
dx = (b - a)/accuracy
integration = 0
for x in np.linspace(a, b, accuracy):
integration += f(x) * np.sin((n * np.pi * x)/L)
integration *= dx
return (1/L) * integration
# Fourier series.
def Sf(x, L, n = 10):
a0 = a(0, L)
sum = np.zeros(np.size(x))
for i in np.arange(1, n + 1):
sum += ((a(i, L) * np.cos((i * np.pi * x)/L)) + (b(i, L) * np.sin((i * np.pi * x)/L)))
return (a0/2) + sum
# x axis.
py.plot(x, np.zeros(np.size(x)), color = 'black')
# y axis.
py.plot(np.zeros(np.size(x)), x, color = 'black')
# Original signal.
py.plot(x, f(x), linewidth = 1.5, label = 'Signal')
# Approximation signal (Fourier series coefficients).
py.plot(x, Sf(x, L), '.', color = 'red', linewidth = 1.5, label = 'Fourier series')
# Specify x and y axes limits.
py.xlim([0, 5])
py.ylim([-2.2, 2.2])
py.legend(loc = 'upper right', fontsize = '10')
py.show()
यदि आप अपना कोड डीबग करते हैं तो आप और जानेंगे। (ऐसा लगता है कि आप इन चीजों को सीखने के अभ्यास के रूप में देखते हैं, लेकिन फिर उस बिंदु पर जहां आप सबसे ज्यादा सीखेंगे, और आपको वास्तव में सोचना होगा, आप अपने उद्देश्य को हराकर SO को एक प्रश्न पोस्ट करते हैं।) – tom10
मैं ' मैं एक ऑटोडिडैक्ट हूं और मैं केवल एक प्रश्न पोस्ट करता हूं अगर मैं अपने साथ लड़ाई खो देता हूं। डिबगिंग के बारे में टिप के लिए धन्यवाद। मैंने अभी तक पायथन के साथ इसका उपयोग नहीं किया है। – bluevoxel