  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Calculate the Fourier series with the trigonometry approach  » python » Calculate the Fourier series with the trigonometry approach

By : user2953695
Date : November 21 2020, 01:01 AM
With these it helps Consider developing your code in a different way, block by block. You should be surprised if a code like this would work at the first try. Debugging is one option, as @tom10 said. The other option is rapid prototyping the code step by step in the interpreter, even better with ipython.
Above, you are expecting that b_1000 is non-zero, since the input f(x) is a sinusoid with a 1000 in it. You're also expecting that all other coefficients are zero right? code : ## How to calculate a Fourier series in Numpy?

By : user2482111
Date : March 29 2020, 07:55 AM
I wish this help you In the end, the most simple thing (calculating the coefficient with a riemann sum) was the most portable/efficient/robust way to solve my problem:
code :
``````def cn(n):
c = y*np.exp(-1j*2*n*np.pi*time/period)
return c.sum()/c.size

def f(x, Nh):
f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/period) for i in range(1,Nh+1)])
return f.sum()

y2 = np.array([f(t,50).real for t in time])

plot(time, y)
plot(time, y2)
`````` ## Fourier Series from Discrete Fourier Transform

By : user3015790
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'm trying to recreate a function from a discrete fourier transform. In Matlab it would be done like this: , You were running two nested loops instead of one. Try this:
code :
``````y = ([(dft[nn].real)*np.cos(np.pi*x*nn) + (dft[nn].imag)*np.cos(np.pi*x*nn) for nn in range(0,n)])
`````` ## Trigonometry functions from series expansion

By : Manoj
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can avoid recalculating x**n and the factorial at each step by calculating the next term of the sum using the previous one:
code :
``````def sin2(x, n=20):
curr =  x
res = curr
for i in range(2, n, 2):
curr *= - x**2/(i*(i+1))
res += curr
return res
``````
``````from math import factorial

def sin(x, n=20):
return sum(x**j/factorial(j)*(1 if i%2==0 else -1)
for i, j in enumerate(range(1, n, 2)))

%timeit sin(0.7)
# 100000 loops, best of 3: 8.52 µs per loop
%timeit sin2(0.7)
# 100000 loops, best of 3: 4.54 µs per loop
``````
``````def sin3(x, n=20):
curr =  x
res = 0
minus_x_squared = - x**2
for i in range(2, n, 2):
res += curr
curr *= minus_x_squared/(i*(i+1))
return res

%timeit sin2(0.7)
# 100000 loops, best of 3: 4.6 µs per loop

%timeit sin3(0.7)
# 100000 loops, best of 3: 3.54 µs per loop
`````` ## Modeling a Fourier Series from Discrete Fourier Transform for Extrapolation

By : user1429804
Date : March 29 2020, 07:55 AM
should help you out The error I see in the code is in the complex multiplication: you multiply the real component of the frequency-domain sample with cos, and the imaginary component with sin. This is not how the complex multiplication works. You need to multiply the complex sample value with the complex value cos + i sin. The complex numbers a+ib and c+id, when multiplied, yield ac-bd+iad+ibc, not ac+bd.
code :
``````N = 250
y = np.cos(np.arange(N)+5)
yf = fft(y)
yf = np.concatenate((yf[:(N+1)//2], np.zeros(N), yf[(N+1)//2:]))
y2 = ifft(yf)
plt.subplot(2,1,1)
plt.plot(y,'.-')
plt.subplot(2,1,2)
plt.plot(y2,'.-')
plt.show()
`````` ## r programming for loop not working for time series with fourier series

By : user3402506
Date : March 29 2020, 07:55 AM
With these it helps The issue seems to be connected with the iterator name i. When I use another iterator, the code works just fine. Unfortunately, I do not exactly understand why this is the case though.
code :
``````for(j in c(1,2,3,4,5)){
fit <- tslm(Gas.train~trend + fourier(Gas.train, K = j))
cat("k = ", j, sep = " ")
print(CV(fit))
}
`````` 