I am using numpy/scipy to plot graphs of sine waves. frequencies at 15 Hz or higher give nice, sharp peaks, but at lower frequencies, the peaks are smeared and the actual peak frequency can't be seen in the graph. Why is that happening and how do i stop that?

    import numpy as np
    import matplotlib.pyplot as plt

    tmin = 0
    tmax = 0.5
    N = 10000 #No. of samples
    t = np.linspace(tmin, tmax, N)
    T = t[1]-t[0] #sampling interval
    fs = 1/T #sampling frequency
    # x1 = np.sin(2*np.pi*5*t) + 0.8*np.sin(2*np.pi*10*t)
    x1 = np.sin(2*np.pi*20*t) + np.sin(2*np.pi*30*t) + np.sin(2*np.pi*5*t) + np.sin(2*np.pi*3*t)
    x2 = x1 + np.sin(2*np.pi*50*t) + 0.8*np.sin(2*np.pi*100*t)
    x2_fft = fft(x2)
    plt.title(r'fft of $x_2(t)$')

    xf = np.linspace(0, fs/2.0, N//2) #Create frequency axis
    plt.plot(xf, 2.0/N * np.abs(x2_fft[0:N//2])) #plot only +ve frequencies
    plt.xlim([0, 120])
    plt.xlabel('Frequency (Hz)')
    plt.title(r'fft of $x_2(t)$')
  • $\begingroup$ They seem to get sharper if the interval is increased. $\endgroup$
    – azman
    Sep 16 '19 at 20:50
  • $\begingroup$ How many periods of a function must a window have before it becomes clear? $\endgroup$
    – azman
    Sep 16 '19 at 20:51
  • $\begingroup$ Define "clear" and "sharp" as per your requirements. $\endgroup$
    – hotpaw2
    Sep 17 '19 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.