python,numpy,scipy,filtering,fft

So I began to think about that, and after a time I realized the stupidity in my ways. My base data is an image, and I take a slice of it to generate the above signal. So instead of trying to implement a less-than-satisfying home-brewed FFT filter to smoooth the...

This can be done by using maximum log likelihood estimation. This is a method that finds the set of parameters that is most likely to have yielded the measured data - the technique originates in statistics. I have finally found an understandable source for how to apply this to binned...

concatenate takes a tuple: yc = np.concatenate((y1c, y2c)) ...

You are right there is something wrong. One needs to explictiy ask pandas for the zeroth column: Hn = np.fft.fft(Moisture_mean_x[0]) Else something wrong happen, which you can see by the fact that the FFT result was not symetric, which should be the case for real input. ...

signal-processing,fft,pitch-tracking,pitch-detection

For Cepstrum i always have used to this steps: Apply hamming windows in the signal (1024 or 2048 points) Apply FFT Get magnitude use just the first half values Convert to log scale Apply IFFT Find the Peak The equation for cepstrum: IFFT(log(abs(FFT(s)))) Maybe are you are seeing reflected because...

analyser.getByteFrequencyData returns a normalized array of values between 0 and 255. The length of the array is half the value of analyzer.fftSize. So if analyzer.fftSize = 1024 analyser.getByteFrequencyData will return an array with 512 values. Also see http://stackoverflow.com/a/14789992/4303873 for more information....

python,numpy,matplotlib,signal-processing,fft

Actually, the opposite. All the other frequencies in a full complex FFT result of strictly real data are split into 2 result bins and mirrored as complex conjugated, thus half a pure sinusoids amplitude when scaled by 1/N, except the DC component and N/2 cosine component, which are not split...

You need to implement two different methods of converting. One for int32 to float and another from int16 to float. As currently implemented it is using the int32 conversion in the int16 case. One problem with this is that the scaling factor for conversion to float is wrong. The other...

I'm not 100% sure what you are using to generate those plots. Generally - if you convert the FFT to a polar format it's much easier to interpret. You end up with one graph for the phase response and one for the frequency response. I'd recommend a read of this:...

The discrete Fourier transform does not have a notion of left and right channels. It takes a time domain signal as a complex valued sequence and transforms it to a frequency domain (spectral) representation of that signal. Most time domain signals are real valued so the imaginary part is zero....

There are a few issues that I can see. You should properly indent your code for readability. Any time you're having trouble, do proper error checking. It's a nitpick, but you didn't check the return code of the call to cufftPlanMany. You also aren't doing proper error checking on the...

matlab,signal-processing,fft,wavelet

Replace the last four lines by hold on %// this prevents each subsequent `plot` from removing the previous graph plot(t1,x1,'r'); %// use appropriate "t" vector: `t1` in this case plot(t2,x2,'g'); plot(t3,x3,'b'); plot(t4,x4,'black'); ...

As I have already stated in my earlier comments, the variable N affects the resolution achievable by the output frequency spectrum and not the range of frequencies you can detect.A larger N gives you a higher resolution at the expense of higher computation time and a lower N gives you...

I figured out the best way is to do changes in the onselect function. So the new source code would be: # -*- coding: utf-8 -*- """ Created on Tue May 19 10:45:47 2015 @author: greenthumbtack """ import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import SpanSelector fig,axarr...

now the problem I am having understanding is what is happening when the int is added to the cplx Actually the addition is not <int> + <cplx> but <int> + <cplx>* (in words an integer plus a pointer to a complex). So what you're dealing with here is what's...

You have a wrongly defined sinc function, as when t=0 it outputs NaN. you can check that doing any(isnan(mt)) in your code. To define properly the function do mt(find(t==0))=1; That will make your code output You may want to reconsider the parameters in order to see better the square wave....

signal-processing,fft,software-defined-radio

Frequency domain entropy, also known as a (Power) Spectral Entropy calculation is done in following steps: Calculate the FFT of your signal. Calculate the PSD of your signal by simply squaring the amplitude spectrum and scaling it by number of frequency bins. Normalize the calculated PSD by dividing it by...

c#,matlab,signal-processing,fft

abs(fft(x)) is not the same as abs(real(fft(x))). You aren't properly combining the real and imaginary parts. Assuming this is the documentation: Compute the forward or inverse Fourier Transform of data, with data containing real valued data only. The output is complex valued after the first two entries, stored in alternating...

audio,numpy,fft,spectrum,nyquist

What you are asking for is not possible. As you mentioned in a comment you require a short time window. I assume this is because you're trying to detect when a signal arrives at a certain frequency (as I've answered your earlier question on the subject) and you want the...

matlab,image-processing,fft,dft

You just need to cast the data to double, then run your code again. Basically what the error is saying is that you are trying to mix classes of data together when applying a matrix multiplication between two variable. Specifically, the numerical vectors and matrices you define in dft1 are...

java,signal-processing,fft,audio-fingerprinting,windowing

At first, I think you should consider having your FFT length fixed. If I understand your code correctly, you are now using some kind of minimum buffer size also as the FFT length. FFT length has huge effect on the performance and resolution of your calculation. Your link to WindowFunction.java...

You have more opening brackets ( than closing ones ), that's a syntax error. It should be: plot(freq1, abs(fft1/max(fft1)),xlabel('f(Hz)'), ylabel('Amplitude I(f)')); ...

python,arrays,opencv,numpy,fft

I changed plt.imshow(.....) into plt.plot(.....) and the script is now working! import cv2 import numpy as np from matplotlib import pyplot as plt squareimpulse = np.array([0,0,0,0,0,1,1,1,1,1,0,0,0,0,0]) img = (squareimpulse) f = np.fft.fft(img) fshift = np.fft.fftshift(f) magnitude_spectrum = (np.abs(fshift)) plt.subplot(121) plt.plot(img) plt.title('Input Image') plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.plot(magnitude_spectrum) plt.title('Magnitude Spectrum') plt.xticks([]), plt.yticks([])...

matlab,signal-processing,fft,frequency,continuous-fourier

I can't get your pictures to load over my proxy, but the spectrum of a FFT will be have a bigger "gap" in the middle at a higher sampling rate. A fundamental property of sampling is that it introduces copies of your original spectrum; you may have learned this if...

The cross-correlation of two complex function equals the convolution of one function and the complex conjugate of the other: Cross correlation and convolution As the function convolve in R already uses the Fast Fourier Transform, all you have to do is: convolve(my.vector1, my.vector2) The maximum lag can be found by:...

java,processing,fft,visualization,music

It turns out what I had to do was multiply the data by Math.log(i + 2) / 3 where i is the index of the data being referenced, zero-indexed from the left (bass). You can see this in context here

There are two problems with your computation: First, you evaluate the time domain filter coefficients on a very narrow time window, which truncates the filter and changes its characteristics. Second, you do not properly keep track of which indices in the time domain and frequency domain vectors correspond to time...

Use numpy. As an example, let me show how I analysed the frequencies in a stereo WAV file; First I read the data and separated it in the left and right channels; import wave import numpy as np wr = wave.open('input.wav', 'r') sz = 44100 # Read and process 1...

objective-c,signal-processing,fft,accelerate-framework

This loop is wrong: for(int i = 0;i<fftLength * 2;i++){ fft[i] = fftData.realp[i]; fft[i+1] = fftData.imagp[i]; } Assuming you want interleaved real/complex output data then it should be: for(int i = 0; i < fftLength; i++) { fft[i * 2] = fftData.realp[i]; fft[i * 2 + 1] = fftData.imagp[i]; }...

arrays,fortran,fft,fortran90,gfortran

Subroutine rdft() seems to use a(0:n-1) according to the declaration subroutine rdft(n, isgn, a, ip, w) integer n, isgn, ip(0 : *), nw, nc real*8 a(0 : n - 1), w(0 : *), xi but the actual arguments seem inconsistent because N_fft is 2*size(curvefft). integer,parameter :: n=1024 real(8), dimension(0:n-1) ::...

The calculation you are making is basically a convolution and convolution in the time domain is simply multiplication in the frequency domain.So just get the FFT of a and multiply it with itself, then perform an IFFT to return to the time domain.So in short, you can calculate b by...

matlab,audio,fft,audio-recording

Here is a implementation using asynchron recording. This way recording the audio data runs in the background and the execution of the code is only paused when no data is available. With nearly 100% of the cpu time available to process the data, it came out that processing was possible...

python,opencv,numpy,fft,correlation

Yup, just like most things I've thought to accomplish by looping over the array: numpy has a built-in solution. [numpy.nonzero][1] numpy.nonzero(a) Return the indices of the elements that are non-zero. Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that...

python,image-processing,filtering,fft

I found the error I made in the end. I made a silly mistake by implementing H(u,v) instead of the correct H(u-M/2,v-N/2).

java,android,fft,frequency-analysis

One of the issues of a DFT is that if your peak is wide and lies over two (or more) bins and it shifts ever so slightly (because of doppler or other reasons), you'll get fluctuations of energy between the two bins. One way is to increase the number of...

In frequency space point value may depend on very image-distant points. So when you do such kind of FT manipulations it is like using multi-points scheme in image space, and for image-space convolution derivatives it is uncommon due to numerical instability. It is a stability-precision trade-off, so for typical non-rocket-science-problems...

c,multidimensional-array,fft,fftw

I ended up solving this problem by making separate real and imaginary 2D arrays. Functions were then written to take in the arrays, create double length 1D arrays with alternating real and imaginary values representing the rows and columns. This allowed me to use the simple in-code FFT four1 function...

c#,fft,naudio,audio-processing

Here's a modified version of your code. Note the comments starting with "***". OpenFileDialog file = new OpenFileDialog(); file.ShowDialog(); WaveFileReader reader = new WaveFileReader(file.FileName); byte[] data = new byte[reader.Length]; reader.Read(data, 0, data.Length); samepleRate = reader.WaveFormat.SampleRate; bitDepth = reader.WaveFormat.BitsPerSample; channels = reader.WaveFormat.Channels; Console.WriteLine("audio has " + channels + " channels, a...

Your problem seems to be that your time-grid is not evenly-spaced: In [83]: d = np.diff(data[:,0]) In [84]: d Out[84]: array([ 0.0006144 , 0.0006144 , 0.00049152, ..., 0.0006144 , 0.0006144 , 0.00049152]) If I interpolate the values to a constant-spacing in time: data = np.loadtxt('3.dat', comments="#") t = data[:, 0]...

matlab,filtering,signal-processing,fft

You're trying to do Deconvolution process by assuming the Filter Model is Gaussian Blur. Few notes for doing Deconvolution: Since your data is real (Not synthetic) data it includes some kind of Noise. Hence it is better to use the Wiener Filter (Even with the assumption of low variance noise)....

The fft is a numerical algorithm that accepts numeric inputs, not symbolic. I suspect that you are declaring t before hand as symbolic. Simply move that t assignment, as well as defining the sampling frequency Fs, sampling time Ts and the maximum time Tmax at the beginning of your code...

matlab,signal-processing,fft,ifft

Well, as the error suggests you have "too many output arguments". By looking at your code I believe that the problem is that audiowrite does not return any output arguments (have a look at http://www.mathworks.com/help/matlab/ref/audiowrite.html). You should use audiowrite(filename,audio_r,44100); instead. In any case, you should learn how to use the...

matlab,filtering,fft,lowpass-filter

Just to remind ourselves of how MATLAB stores frequency content for Y = fft(y,N): Y(1) is the constant offset Y(2:N/2 + 1) is the set of positive frequencies Y(N/2 + 2:end) is the set of negative frequencies... (normally we would plot this left of the vertical axis) In order to...

matlab,function,plot,fft,frequency-analysis

fft returns spectrum as complex numbers. In order to analyze it you have to use its absolute value or phase. In general, it should look like this (let's assume that t is vector containing time and y is the one with actual signal, N is the number of samples): fY...

I'm guessing your signals aren't actually the same length. If you're thresholding them independently, your thresh_start value won't be the same, so: onesec = x[thresh_start-1:one_sec] will give you different-length arrays for the two files. You can either calculate the threshold value separately and then provide that number to this module...

Ok, it was my lack of understanding the FFT... I changed the getComplexArray method to the following and now it works fine: public static Complex[] getComplexArray(double[] input) { Deque<Complex> deque = new LinkedList<Complex>(); int size = (input.length / 4 + 1) * 2; for (int i = 0; i <...

c++,fft,frequency-analysis,unreal-engine4

I don't see a Fourier transform to be carried out in your code snippet. Any way, using a DFT given N samples at an average sampling frequency R, the frequency corresponding to the bin k is k·R/2N

As is typical of routines computing discrete fourier transform of real-valued sequences, the resulting complex valued spectrum is returned for only half the spectrum. To fit the values into the original N-element array, only the real-part of the first value (which is always real) is returned. Similarly in the case...

For high frequencies, you can try using the Goertzel algorithm. Compare the energy output of a Goertzel filter with the total audio energy over the same time period, and see whether that meets some threshold or statistical decision criteria. For lower pitched frequencies, to solution may not be easier, but...

c,algorithm,signal-processing,fft,ifft

I strongly recommend to read this: http://stackoverflow.com/a/26355569/2521214 Now from the first look the offset and delta is used to: make the butterfly shuffle permutation you start with step 1 and half of the interval and by recursion you will get to log2(N) step and interval of 1 item ... +/-...

c#,audio,signal-processing,fft

I ended up using this example which works really well. I wrapped it a bit nicer and ended up with: /// <summary> /// Returns a low-pass filter of the data /// </summary> /// <param name="data">Data to filter</param> /// <param name="cutoff_freq">The frequency below which data will be preserved</param> private float[] lowPassFilter(ref...

This line write(1,*) k, wynik writes out the value of the scalar k and the entire rank-1 array wynik. In these two lines integer, parameter :: nmax=10 ... real(kind=10), dimension(0:nmax) :: f, wynik wynik is declared to have 11 elements. So, naturally, the program writes out one value for k...

You can use logical indexing: a = randn(1,1000); fs=10; ydft = fft(a); ydft = ydft(1:length(a)/2+1); freq = 0:fs/length(a):fs/2; lowestFrequencyToPlot = 2; idxHigherFrequencies = freq >= lowestFrequencyToPlot; plot(freq(idxHigherFrequencies),abs(ydft(idxHigherFrequencies))); only the highest frequency can be plotted with end. Edit: The array freq will consist of the frequencies like: [1,2,3,4,5]. If you compare...

machine-learning,fft,data-mining,similarity,feature-extraction

Generally you should extract just a small number of "Features" out of the complete FFT spectrum. First: Use the log power spec. Complex numbers and Phase are useless in these circumstances, because they depend on where you start/stop your data acquisiton (among many other things) Second: you will see a...

There is usually an associated function in instances like this. In this case PeriodogramArray outputs the data. data = Table[ 2 Sin[0.2 Pi n ] + Sin[0.5 Pi n] + RandomReal[{-1, 1}], {n, 0, 127}]; Periodogram[data] magdata = PeriodogramArray[data]; ListLinePlot[10 Log[10, magdata], PlotRange -> {{0, Length[magdata]/2}, All}] ...

python,numpy,signal-processing,fft

The mean value of the von Hann window is (approximately) 0.5, for N=1000 you have >>> N=1000 ; print sum(np.hanning(N))/N 0.4995 >>> Does this explain the necessity of multiplying by two to recover the discrete amplitudes?...

python,image-processing,numpy,fft,edge-detection

If you find the 2D FFT method unsatisfactory, you might consider attacking this problem using opencv since the toolkit is highly developed and provides many tools suitable for the problem you describe. One potential strategy: construct an image pyramid from the image in question. Then, perform an edge detection operation...

matlab,audio,signal-processing,fft

That's because you're not plotting the magnitude. What you are plotting are the coefficients, but these are complex valued. Because of that, the horizontal axis is the real component and the vertical axis is the imaginary component. Also, when you use sound by itself, the default sampling frequency is 8...

You are almost there. Just missed to include the imaginary plane which is all zeroes. Mat planes[] = {Mat_<float>(dataPad), Mat::zeros(dataPad.size(), CV_32F)}; Mat complexI; merge(planes, 2, complexI); dft(complexI, complexI, DFT_COMPLEX_OUTPUT); std::cout << complexI ; Of lesser importance is OpenCV's way of padding is to use copyMakeBorder and getOptimalDftSize. Mat dataPadded; //expand...

Like @VladimirF already said, the ordering of the values is a bit different, than you might expect. The first half of the array holds the positive frequencies, the second half holds the negative frequencies in reverse order (see this link). And you might have to check the sign convention used...

Move the FFT initialization (plan creation) outside of the performance critical loop. The setup code has to allocate memory and calculate O(N) transcendental functions, which can be much slower than the O(NlogN) simple arithmetic ops inside the FFT computation itself.

algorithm,matrix,fft,polynomials

You can not expect this method to work if your coefficient matrices do not commute with your matrix step. To get it working correctly, use the diagonal matrix corresponding to multiplication with the scalar exp(i*2*PI/M).

matlab,signal-processing,fft,wavelet,haar-wavelet

Idea: get the axis the was used to plot the spectrogram and set its properties accordingly. For example, supposing you'd want to restrict the x range to [0, 0.5] and y to [100, 200], then: %'old code here' %' . . . ' spectrogram(x4,128,50,NFFT); %'new code here' axis(get(gcf,'children'), [0, 0.5,...

Okay, okay. Due to the generally inferior nature of my mistake the solution is quite trivial. I wrote freq = 200 and duration = 3. But the real duration is from -4pi to 4 pi, hence 8pi resulting in a "real" sample frequency of 1/ ((8*pi)/600) = 23.87324 which does...

c++,algorithm,recursion,iteration,fft

The first thing I'd do is make your function "more recursive". void fft(base* fa, size_t stride, size_t n) { if (n==1) return; int half = (n>>1); fft(fa+stride, stride*2, half); // odd fft(fa, stride*2, half); // even int fact = FFTN/n; for (int i=0;i<half;i++){ fa[i] = fa[stride*2*i] + fa[stride*2+i+stride] * w[i...

Your sum depends only on m so I assume that other parameters as well as function H((m+n)/(NT)) are defined. Via for-loops the simplest one is: function [S]=discrete_s_transform(x); N=length(x); % length of signal S=0; m=0; for i=1:N S=S+H((m+n)/(NT))*exp(a)*exp(b*m/N); m=m+1; end Hope that helps!...

As you have noted, the fftw_plan_dft_1d function computes the standard FFT Yk of the complex input sequence Xn defined as where j=sqrt(-1), for all values k=0,...,N-1 (thus generating N complex outputs in the array out), . You may notice that since the input happens to be real, the output exhibits...

When you compute the FFT of audio (or any data) of length N at a sampling rate fs the result will be an array, Y, of N complex numbers where magnitude(Y(n)) = amplitude at frequency (n * fs)/N Hz. Where n = 0,1,2,...N. meaning that Y contains the information on...

numpy,signal-processing,fft,ifft

The FFT of a real-valued input signal will produce a conjugate symmetric result. (That's just the way the math works best.) So, for FFT result magnitudes only of real data, the negative frequencies are just mirrored duplicates of the positive frequencies, and can thus be ignored when analyzing the result....

I apologize that the following is a bit messy. I did everything manually because I'm not sure how else to do it. First you need to know how MATLAB stores frequency domain data. Take the following example: N = 100; % number of samples Fs = 100; % sampling frequency...

Your code is correct as it is. But your signal, once made periodic, is not just a sine wave (there is a discontinuity, because the 1st and last samples of x are the same). You can try removing 1 sample at the end: t=0:dt:maxtime; % time interval in which we...

The best way to force the fit function on a certain period is to resort to a custom equation model, via fittype. Another option (that will throw a warning) is to fix the lower and upper bounds of the parameter w to the same value, and select as solution method...

matlab,plot,filter,signals,fft

I believe your high frequency components are noise, but it actually depends on your data. See this example, Fs = 2000; L = 200; t = (0 : L - 1)/Fs; data = chirp(t,20,.05,50) + chirp(t,500,.1,700); subplot(411) plot(t,data,'LineWidth',2); title('Original Data') N = 2^nextpow2(L); y = fft(data,N)/L; f = Fs/2 *...

c++,matlab,fft,fftw,frequency-analysis

If you have a real signal consisting of 1024 samples, the contribution from the 16 frequency bins of interest could be obtained by multiplying the frequency spectrum by a rectangular window then taking the IFFT. This essentially amounts to: filling a buffer with zeros before and after the frequency bins...

signal-processing,fft,wavelet,haar-wavelet,wavelet-transform

note that, the width of the winow function is constant throughout the entire STFT process. and the time (t) in the function g(t-t') indicate sthat, t: is the current time on the time axis and it is variable each time the window is moved/shifted to the righ to overlap the...

matlab,signal-processing,fft,wavelet

You can not see what you expected because the value of NFFT is 1 means when you write NFFT/2+1 as an index of Y it will not be an integer value so MATLAB warns you. You can calculate NFFT like this: NFFT = 2^nextpow2(length(t)) instead of writing NFFT = 2^nextpow2(StopTime)...

If x is a 2N signal padded with zeros above N , its DFT writes : If k is even : Hence, the coefficients of even frequencies arise from the N-point discrete Fourier transform of x(n). if k is odd : Hence, the coefficients of odd frequencies arise from the...

At first, your result does not look like a complex FFT output, because you calculated the absolute values of the FFT. That's never complex... Therefore: result = fftpack.fft(targetArray)[0:sample_size//2] (sample_size//2 ensures that the upper bound of the slice is an integer.) Your get_energy-function should actually look like this: def get_energy(input): return...

r,image-processing,fft,cross-correlation

The code looks correct from what I know about phase correlation. If I understand what you want correctly, you are trying to use phase correlation to determine the offset between two images given that their homographies are nothing more than horizontal and vertical offsets. The fact that you're only getting...

The FFT is not normalized, so the first term should be the sum, not the mean. For example, see the definition here and you can see, that when k=0, the exponential term is 1, and you'll just get the sum of x_n. This is why the first item in fft(np.ones(10))...

python,performance,numpy,multiprocessing,fft

Numpy can typically do things hundreds of time faster than plain python, with very little extra effort. You just have to know the right ways to write your code. Just to name the first things I think of: Indexing Plain python is often very slow at things that a computer...

The numpy FFT procedures actually and in contrast to other software do adjust for the sequence length, so that you get nf.ifft(nf.fft(gx)) == gx up to some floating point error. If your dx and dk are computed the usual way, then dk*dx=(2*pi)/N which only works for unadjusted FFT routines. You...

From the documentation (emphasis mine): The wave module provides a convenient interface to the WAV sound format. It does not support compression/decompression, but it does support mono/stereo. You will need to find a module that actually supports decompressing audio....

Why reinvent the wheel? Academic purposes You have time to waste, see 1. You are a code wizard and you know your code is superior in all ways. Why not reinvent the wheel? Speed of development: writing everything from scratch is just not doable Accuracy of result: you are bound...

It's not kiss which causes the problem here. It is how the result array is (mis)handled. To really "keep it simple and stupid" (KISS), I recommend to use STL containers for your data instead of raw c++ arrays. This way, you can avoid the mistakes you did in the code....

matlab,fft,noise,spectrum,mathcad

The main reason why it doesn't work is due to the scaling factor of the FFT between MathCad and MATLAB. With MathCad, there is an extra scaling factor of 1/sqrt(N) whereas MATLAB does not include this said scaling factor. As such, you'll need to multiply your FFT results by this...

To expand on @Oliver Charlesworth, the command fft(X,[],DIM) should do this: fft_values=zeros(size(X)); %pre-allocate the output array if (DIM==1) for I=1:size(X,2) for J=1:size(X,3) fft_values(:,I,J) = fft(squeeze(X(:,I,J)); end end elseif (DIM==2) for I=1:size(X,1) for J=1:size(X,3) fft_values(I,:,J) = fft(squeeze(X(I,:,J)); end end elseif (DIM==3) for I=1:size(X,1) for J=1:size(X,2) fft_values(I,J,:) = fft(squeeze(X(I,J,:)); end end end...

The easiest way I've found to do this is to use some utility functions from the data.table package: fread (a faster file reader) and rbindlist (consolidate a list into a single data table or data frame). Something along these lines should do what you want: files <- list.files(path = path,...

The frequency detection code you linked is performing an FFT and then finding the bin with the largest magnitude. The only way it's going to return a zero is if the largest magnitude is in the 0th bin. Even though you may not be producing a tone at the frequency...

Answer to the Edited Version While this version: from __future__ import print_function import sys if sys.version_info.major < 3: range = xrange from cmath import exp, pi def fft2(x): N = len(x) T = exp(-2*pi*1j/N) if N > 1: x = fft2(x[::2]) + fft2(x[1::2]) for k in range(N//2): xk = x[k]...

The first thing that can be noticed is that the generated series in your plot runs for two periods in the support interval [-pi:pi]. This point to an incorrect constant in your sin(2*n*t) argument, which should instead be sin(n*t). Also, as a general rule odd functions have only sin terms...