banner



How To Plot The Frequency Response Of A Cw Filter In Python

How to Plot the Frequency Response of a Filter

This article shows how to plot the frequency response of the filters that I depict in my articles on filter blueprint. Plotting this kind of frequency response should non be dislocated with the spectral density estimation of measured signals, which tin can exist (much) more involved. For the specific case of a filter, however, the frequency response tells you exactly how each frequency is altered. The example code is in Python, equally usual, just the methodology is applicable for any programming language or plotting tool.

Time and Frequency Representation

The master performance that will get you from the time domain to the frequency domain is the Discrete Fourier Transform (DFT). In exercise, you'll typically use the Fast Fourier Transform (FFT) , which is an efficient algorithm for computing the DFT.

A crucially of import point is that simply calculating the FFT of the filter is non plenty. As an example, Figure 1 shows a low-pass filter, as presented in How to Create a Unproblematic Depression-Pass Filter, both in the time domain (left) and in the frequency domain (correct).

Figure 1. Time domain (left) and frequency domain (right) representation of a filter.Figure one. Fourth dimension domain (left) and frequency domain (right) representation of a filter.

The dots in the frequency domain plot are exactly the result of the FFT, but, past themselves, they don't give you a clear picture of the true frequency response. There are at least three reasons for this:

  1. The frequency response of a discrete-fourth dimension (or digital) filter is continuous, even though the Fourier transform is a finite number of points.
  2. The response that is plotted is typically from 0 Hz to half of the sampling rate. However, the actual result of the FFT contains a mirror of this in its 2nd half (in the more general example of filters with complex coefficients this is no longer true, only I'll keep that for a future commodity).
  3. The plot has a linear scale, while frequency plots by and large have a logarithmic scale (in dB).

As a first pace towards the typical frequency response plots that you lot are probably more familiar with, Figure 2 shows only the beginning half of the FFT, in dB. I accept an commodity on the normalized frequency that is used on the X axis, if you are curious.

Figure 2. Frequency response of filter in dB.Figure two. Frequency response of filter in dB.

Continuous Frequency Response

The dots in Figure ii are samples of the complete frequency response. However, the actual frequency response of the filter is, of course, continuous, since it has an exactly defined effect on every frequency that yous put through the filter. To show the continuous frequency response of the filter, some more work is needed. The way to practise this is to "pad with zeros". You increase the length of the time signal, typically to a power of two such as 1024 (the power of two is only for the FFT algorithm). The reason that y'all are immune to practise this, is that adding zeros does not modify the filter, since the zeros have no outcome. However, the result of the FFT of this longer signal, still merely samples of the true response, will exist smooth when plotted in a graph. The result of padding with zeros to a length of 1024 samples is shown in Figure 3.

Figure 3. Frequency response of padded filter.Figure 3. Frequency response of padded filter.

The plot of Figure 3 is exactly how I normally present frequency response plots. Python lawmaking that creates this plot follows in the next section.

Python Lawmaking

                    
                        from                        __future__                        import                        division                        import                        numpy                        as                        np                        import                        matplotlib.pyplot                        equally                        plt   fc                        =                        0.2                        # Cutoff frequency equally a fraction of the sampling rate (in (0, 0.5)).                        Northward                        =                        25                        # Number of coefficients.                        L                        =                        1024                        # Length of frequency response.                        # Compute sinc filter with Hamming window.                        north                        =                        np.arange                        (Due north)                        h                        =                        np.sinc                        (                        2                        * fc *                        (n -                        (North -                        1                        )                        /                        2                        )                        )                        * np.hamming                        (Northward)                        h /=                        np.sum                        (h)                        # Pad filter with zeros.                        h_padded                        =                        np.zeros                        (L)                        h_padded[                        0                        : N]                        =                        h                        # Compute frequency response; only keep first half.                        H                        =                        np.abs                        (np.fft.fft                        (h_padded)                        )                        [                        0                        : L //                        2                        +                        1                        ]                        # Plot frequency response (in dB).                        plt.figure                        (                        )                        plt.plot                        (np.linspace                        (                        0                        ,                        0.5                        ,                        len                        (H)                        )                        ,                        20                        * np.log10                        (H)                        )                        plt.xlabel                        (                        'Normalized frequency'                        )                        plt.ylabel                        (                        'Gain [dB]'                        )                        plt.ylim                        (                        [-100                        ,                        10                        ]                        )                        plt.grid                        (                        )                        plt.show                        (                        )                      

Submitted on 4 November 2017

How To Plot The Frequency Response Of A Cw Filter In Python,

Source: https://tomroelandts.com/articles/how-to-plot-the-frequency-response-of-a-filter

Posted by: grangerapoing.blogspot.com

0 Response to "How To Plot The Frequency Response Of A Cw Filter In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel