Engee documentation
Notebook

Logarithmic amplitude-phase frequency response

In this example, the following steps are taken to construct a logarithmic amplitude-phase frequency response:

  1. A sinusoid with a given frequency is created.
  2. A fast Fourier transform (FFT) is applied to the signal.
  3. The amplitude and phase are calculated.
  4. The amplitude response in logarithmic scale and phase response are plotted.

Before starting work it is necessary to connect the FFTW library, which contains FFT functions.

In [ ]:
using FFTW

Now let's set the parameters of our signal.

In [ ]:
fs = 1000;    # Частота дискретизации, Гц
T = 1.0;      # Длительность, с
f_sin = 50;   # Частота синусоиды, Гц

Next, set the time sample for our data and generate a sinusoidal signal.

In [ ]:
t = 0:1/fs:T  # Время
x = sin.(2π * f_sin * t)  # Сигнал - синусоида
plot(t,x)
Out[0]:

Apply the fast Fourier transform to the generated signal.

In [ ]:
X = fft(x)
n = length(x)
freqs = (0:n-1) * (fs / n)
Out[0]:
0.0:0.999000999000999:999.000999000999

Calculate the amplitude and phase of the input signal.

In [ ]:
amplitude = abs.(X)
plot(amplitude)
Out[0]:
In [ ]:
phase = angle.(X)
plot(phase)
Out[0]:

Plot the amplitude response in logarithmic scale.

In [ ]:
plot(freqs[1:div(n, 2)], 20 * log10.(amplitude[1:div(n, 2)]), title="Amplitude Response (dB)", xlabel="Frequency (Hz)", ylabel="Amplitude (dB)", legend=false)
Out[0]:

Let's build the phase response.

In [ ]:
plot(freqs[1:div(n, 2)], phase[1:div(n, 2)], title="Phase Response", xlabel="Frequency (Hz)", ylabel="Phase (radians)", legend=false)
Out[0]:

Conclusion

In this example we have learnt how to plot the logarithmic AFC and FFC for an arbitrary signal.