Inverse Fourier transform in Python not yielding the original signal - python

I am trying to do a Fourier Transform and Inverse Fourier Transform. The problem is that I am not getting the original signal after inverse transform.
fig = plt.gcf()
fig.set_size_inches(16,10)
# set signal and transform constants
samples = 10000
t = np.arange(samples)
cf = 1000 # carrier frequency Hz
mf = 60 # modulation frequency Hz
# signal is a classic AM waveform
signal = lambda t: np.sin(2 * np.pi * cf * t / samples) \
* (1+(np.sin(2 * np.pi * mf * t / samples)))
sp1 = plt.subplot(121)
sp1.set_xlim(0,512)
sp1.set_ylabel('Amplitude')
sp1.set_title('Time Domain')
sp1.set_xlabel('Time(s)')
sp1.plot(t,signal(t))
plt.grid(color='grey', linestyle='-.', linewidth=0.5)
sp2 = plt.subplot(122)
sp2.set_xlabel('Frequency Hz')
sp2.set_ylabel('Amplitude')
sp2.set_title('Frequency Domain')
sp2.set_xlim(500,1500)
fi = np.arange(5000)
fs = np.fft.fft(signal(t))[:5000]
sp2.plot(fi ,abs(fs) * 2 / samples, color = "#fb8072")
plt.grid(color='grey', linestyle='-.', linewidth=0.5)
plt.show()
fig = plt.gcf()
fig.set_size_inches(16,10)
# set signal and transform constants
samples = 10000
t = np.arange(samples)
# declare an all-zeros frequency spectrum
s = np.zeros((samples,), dtype=complex)
# set the spectral lines
s[940] = 0.5
s[1000] = 1.0
s[1060] = 0.5
plt.grid(True)
sp1 = plt.subplot(121)
sp1.set_xlim(500,1500)
sp1.set_ylabel('Amplitude')
sp1.set_title('Frequency Domain')
sp1.plot(t,s,color='#80b1d3')
plt.grid(color='grey', linestyle='-.', linewidth=0.5)
sp2 = plt.subplot(122)
sp2.set_ylabel('Amplitude')
sp2.set_title('Time Domain')
fi = np.arange(512)
fs = np.fft.ifft(s)[:512]
sp2.plot(fi, fs * samples ,color='#fdb462')
plt.grid(color='grey', linestyle='-.', linewidth=0.5)
plt.show()
I have tried looking at the real and imaginary parts of the signal but none of them looks like the original signal.

Related

How to plot upper and lower boundary with a LINEAR line on a scatter plot?

I have a data frame df with columns A and Q. I am using this code to draw a line of equation on it.
#Actual line of equation, which has to be plotted: Q=alpha*A^beta : ln(Q)=a+b*ln(A) : y = a+b(x)
x = np.log(df['A'])
y = np.log(df['Q'])
#deriving b,a
b,a = np.polyfit(np.log(x), y, 1)
#deriving alpha and beta. By using a = ln(alpha); b = beta -1
alpha = np.exp(a)
beta = b + 1
Q = df['Q'].values
A = df['A'].values
#equation of line
q = alpha * np.power(A,beta)
#plotting the points and line
plt.scatter(A,Q)
plt.plot(A,q, '-r')
plt.yscale('log')
plt.xscale('log')
This gives the following output, which is similar to a regression line.
But I am interested in plotting the same line of the equation as the upper and lower curve/boundary joining the farthest points(perpendicular to the green line) on both sides as shown below with the same slope as that of the continuous green line.
The idea is to first search the index of the point where the difference between the line and the plot is minimal (cf. maximal). With this point, alpha_min can be calculated such that
Q[pos_min] == alpha_min * np.power(A[pos_min], beta), thus
alpha_min = Q[pos_min] / np.power(A[pos_min], beta).
As such lines can extend quite far away from the original points, it can help to restore the x and y limits (thus clipping the plot to the original region).
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame()
df['A'] = 10 ** np.random.uniform(0, 1, 1000) ** 2
df['Q'] = 10 ** np.random.uniform(0, 1, 1000) ** 2
x = np.log(df['A'])
y = np.log(df['Q'])
# deriving b,a
b, a = np.polyfit(np.log(x), y, 1)
# deriving alpha and beta. By using a = ln(alpha); b = beta - 1
alpha = np.exp(a)
beta = b + 1
Q = df['Q'].values
A = df['A'].values
# plotting the points and line
plt.yscale('log')
plt.xscale('log')
plt.scatter(A, Q, color='b')
# equation of line
xmin, xmax = plt.xlim() # the limits of the x-axis for drawing the line
x = np.linspace(xmin, xmax, 50)
q = alpha * np.power(x, beta)
plt.plot(x, q, '-r')
ymin, ymax = plt.ylim() # store the limits of the scatter and line plot so they can be restored later
pos_min = np.argmin(Q / np.power(A, beta))
pos_max = np.argmax(Q / np.power(A, beta))
alpha_min = Q[pos_min] / np.power(A[pos_min], beta)
alpha_max = Q[pos_max] / np.power(A[pos_max], beta)
# plt.scatter(A[pos_min], Q[pos_min], s=100, fc='none', ec='r', lw=3)
# plt.scatter(A[pos_max], Q[pos_max], s=100, fc='none', ec='g', lw=3)
plt.plot(x, (alpha_max) * np.power(x, beta), '--r')
plt.plot(x, (alpha_min) * np.power(x, beta), '--r')
plt.xlim(xmin, xmax) # restore the limits of the scatter plot
plt.ylim(ymin, ymax)
plt.show()

How to plot frequency band using `matplotlib.pyplot.specgram`

I can plot a spectrogram (in a Jupyter notebook) thus:
fs = 48000
noverlap = (fftFrameSamps*3) // 4
spectrum2d, freqs, timePoints, image = \
plt.specgram( wav, NFFT=fftFrameSamps, Fs=fs, noverlap=noverlap )
plt.show()
However, I am only interested in the 15-20 kHz range.
How can I plot only this range?
I can see that the function returns image, so maybe I could convert the image to a matrix and take an appropriate slice from the matrix...?
I can see that the function accepts vmin and vmax but these appear to be undocumented and playing with them doesn't yield a valid result.
You can modify the limits of the axis as you would normally with set_ylim() and set_xlim(). In this case
plt.ylim([15000, 20000])
should restrict your plot to the 15-20 kHz range. For a complete example drawing from the Spectrogram Demo:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
dt = 0.0005
t = np.arange(0.0, 20.0, dt)
s1 = np.sin(2 * np.pi * 100 * t)
s2 = 2 * np.sin(2 * np.pi * 400 * t)
# create a transient "chirp"
s2[t <= 10] = s2[12 <= t] = 0
# add some noise into the mix
nse = 0.01 * np.random.random(size=len(t))
x = s1 + s2 + nse # the signal
NFFT = 1024 # the length of the windowing segments
Fs = int(1.0 / dt) # the sampling frequency
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(14, 7))
ax1.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
ax2.set_ylim([50, 500])
plt.show()

Filter out range of frequencies using band stop filter in Python and confirm it using Fourier Transform FFT

Supposing that I have following signal:
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(100.0 * 2.0*np.pi*x) + 0.2*np.sin(200 * 2.0*np.pi*x)
how can I filter out in example 100Hz using Band-stop filter in Python? In this signal there are peaks at 50Hz, 100Hz and 200Hz. It would be helpful it it could be visualized using FFT in order to confirm that this frequency has been filtered correctly.
Basing on answers from:
Plotting a Fast Fourier Transform in Python
and:
Bandstop filter
I wrote following code:
import pandas as pd
import time
from scipy.signal import lfilter
import matplotlib.pyplot as plt
import scipy
import numpy as np
# In the below lines data are being filtered using Bandstop filter
print("Filtering using Bandstop filter...")
start_filtering_bandstop = time.time()
# Define filtering parameters:
order = 2
fs = 800.0 # sample rate, Hz
lowcut = 90 # desired cutoff frequency of the filter, Hz
highcut = 110
# Define plots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 7))
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / fs
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(100.0 * 2.0*np.pi*x) + 0.2*np.sin(200 * 2.0*np.pi*x) # You can put there pandas series too...
ax1.plot(x, y, label='Signal before filtering')
print("Calculating FFT, please wait...")
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
ax1.set_title('Signal')
ax2.set_title('FFT')
ax2.plot(xf, 2.0/N * np.abs(yf[:N//2]), label='Before filtering')
def butter_bandstop_filter(data, lowcut, highcut, fs, order):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = scipy.signal.butter(order, [low, high], btype='bandstop')#, fs, )
y = lfilter(b, a, data)
return y
print("Filtering signal, please wait...")
signal_filtered = butter_bandstop_filter(y, lowcut, highcut, fs, order)
ax1.plot(x, signal_filtered, label='Signal after filtering')
ax1.set(xlabel='X', ylabel='Signal values')
ax1.legend() # Don't forget to show the legend
ax1.set_xlim([0,0.8])
ax1.set_ylim([-1.5,2])
# Number of samplepoints
N = len(signal_filtered)
# sample spacing
T = 1.0 / fs
x = np.linspace(0.0, N*T, N)
y = signal_filtered
print("Calculating FFT after filtering, please wait...")
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
# Plot axes...
ax2.plot(xf, 2.0/N * np.abs(yf[:N//2]), label='After filtering')
ax2.set(xlabel='Frequency [Hz]', ylabel='Magnitude')
ax2.legend() # Don't forget to show the legend
plt.savefig('FFT_after_bandstop_filtering.png', bbox_inches='tight', dpi=300) # If dpi isn't set, the script execution will be faster
# Alternatively for immediate showing of plot:
# plt.show()
plt.close()
end_filtering_bandstop = time.time()
print("Data filtered using Bandstop filter in",round(end_filtering_bandstop - start_filtering_bandstop,2),"seconds!")
and obtained following plots:
As we can see, the 100Hz has been filtered out using band-stop filter.
Why magnitude for frequency 50 Hz decreased from 1 to 0.7 after Fast Fourier Transform?

How to remove frequency from signal

I want to remove one frequency (one peak) from signal and plot my function without it. After fft I found frequency and amplitude and I am not sure what I need to do now. For example I want to remove my highest peak (marked with red dot on plot).
import numpy as np
import matplotlib.pyplot as plt
# create data
N = 4097
T = 100.0
t = np.linspace(-T/2,T/2,N)
f = np.sin(50.0 * 2.0*np.pi*t) + 0.5*np.sin(80.0 * 2.0*np.pi*t)
#plot function
plt.plot(t,f,'r')
plt.show()
# perform FT and multiply by dt
dt = t[1]-t[0]
ft = np.fft.fft(f) * dt
freq = np.fft.fftfreq(N, dt)
freq = freq[:N/2+1]
amplitude = np.abs(ft[:N/2+1])
# plot results
plt.plot(freq, amplitude,'o-')
plt.legend(('numpy fft * dt'), loc='upper right')
plt.xlabel('f')
plt.ylabel('amplitude')
#plt.xlim([0, 1.4])
plt.plot(freq[np.argmax(amplitude)], max(amplitude), 'ro')
print "Amplitude: " + str(max(amplitude)) + " Frequency: " + str(freq[np.argmax(amplitude)])
plt.show()
One option is to transform the signal to the frequency domain then remove the selected frequency.
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import rfft, irfft, fftfreq, fft
# Number of samplepoints
N = 500
# sample spacing
T = 0.1
x = np.linspace(0.0, (N-1)*T, N)
# x = np.arange(0.0, N*T, T) # alternate way to define x
y = 5*np.sin(x) + np.cos(2*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
#fft end
f_signal = rfft(y)
W = fftfreq(y.size, d=x[1]-x[0])
cut_f_signal = f_signal.copy()
cut_f_signal[(W>0.6)] = 0 # filter all frequencies above 0.6
cut_signal = irfft(cut_f_signal)
# plot results
f, axarr = plt.subplots(1, 3, figsize=(9, 3))
axarr[0].plot(x, y)
axarr[0].plot(x,5*np.sin(x),'g')
axarr[1].plot(xf, 2.0/N * np.abs(yf[:N//2]))
axarr[1].legend(('numpy fft * dt'), loc='upper right')
axarr[1].set_xlabel("f")
axarr[1].set_ylabel("amplitude")
axarr[2].plot(x,cut_signal)
axarr[2].plot(x,5*np.sin(x),'g')
plt.show()
You can design a bandstop filter:
from scipy import signal
wc = freq[np.argmax(amplitude)] / (0.5 / dt)
wp = [wc * 0.9, wc / 0.9]
ws = [wc * 0.95, wc / 0.95]
b, a = signal.iirdesign(wp, ws, 1, 40)
f = signal.filtfilt(b, a, f)

FFT coefficients using python

I am a newbie in Signal Processing. In here, I want to ask how to get FFT coeffients from FFT from in python. This is the example of my code:
from scipy.fftpack import fft
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()
Hmm I don't really know about signal processing either but maybe this works:
from scipy.signal import argrelmax
f = xf[scipy.signal.argrelmax(yf[0:N/2])]
Af = np.abs(yf[argrelmax(yf[0:N/2])])
Quoting #hotpaw, in this similar answer:
"The real and imaginary arrays, when put together, can represent a complex array. Every complex element of the complex array in the frequency domain can be considered a frequency coefficient, and has a magnitude sqrt(RR + II))".
So, the coefficients are the complex elements in the array returned by the fft function. Also, it is important to play with the size (the number) of the bins for the FFT function. It would make sense to test a bunch of values and pick the one that makes more sense to your application. Often, it is in the same magnitude of the number of samples. This was as assumed by most of the answers given, and produces great and reasonable results. In case one wants to explore that, here is my code version:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
fig = plt.figure(figsize=[14,4])
N = 600 # Number of samplepoints
Fs = 800.0
T = 1.0 / Fs # N_samps*T (#samples x sample period) is the sample spacing.
N_fft = 80 # Number of bins (chooses granularity)
x = np.linspace(0, N*T, N) # the interval
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x) # the signal
# removing the mean of the signal
mean_removed = np.ones_like(y)*np.mean(y)
y = y - mean_removed
# Compute the fft.
yf = scipy.fftpack.fft(y,n=N_fft)
xf = np.arange(0,Fs,Fs/N_fft)
##### Plot the fft #####
ax = plt.subplot(121)
pt, = ax.plot(xf,np.abs(yf), lw=2.0, c='b')
p = plt.Rectangle((Fs/2, 0), Fs/2, ax.get_ylim()[1], facecolor="grey", fill=True, alpha=0.75, hatch="/", zorder=3)
ax.add_patch(p)
ax.set_xlim((ax.get_xlim()[0],Fs))
ax.set_title('FFT', fontsize= 16, fontweight="bold")
ax.set_ylabel('FFT magnitude (power)')
ax.set_xlabel('Frequency (Hz)')
plt.legend((p,), ('mirrowed',))
ax.grid()
##### Close up on the graph of fft#######
# This is the same histogram above, but truncated at the max frequence + an offset.
offset = 1 # just to help the visualization. Nothing important.
ax2 = fig.add_subplot(122)
ax2.plot(xf,np.abs(yf), lw=2.0, c='b')
ax2.set_xticks(xf)
ax2.set_xlim(-1,int(Fs/6)+offset)
ax2.set_title('FFT close-up', fontsize= 16, fontweight="bold")
ax2.set_ylabel('FFT magnitude (power) - log')
ax2.set_xlabel('Frequency (Hz)')
ax2.hold(True)
ax2.grid()
plt.yscale('log')
Output:

Categories