Configuring the filter parameters via a custom form
Let's build a small application (a cell mask) that will help us select filter parameters without leaving the interactive script.
Uploading data
Suppose we have the following signal:
Pkg.add(["Statistics", "DSP", "CSV"])
gr()
using CSV, DataFrames
d = CSV.read( "$(@__DIR__)/scope_data.csv", DataFrame );
Let's take the average difference between the time steps as the sampling frequency.
using Statistics
Fs = 1 / mean(diff(d.Time)); # Sampling rates
Fn = Fs/2; # Nyquist Frequency
L = length( d.Time ); # The number of samples in the signal
And let's try to build a filter that meets some technical requirements.
using FFTW, DSP
Fl = round.(Int, round(L/2)+1) # The number of points in the discrete Fourier transform
Fv = range(1/Fl, 1, length=Fl) .* Fn; # Frequency vector (excluding the first point – 0)
Iv = 1:length(Fv); # Index vector
# @markdown ## Configuring filter parameters
# Default values
pT, pB, sT, sB, Rp, Rs = 20, 30, 18, 35, 1, 50;
# @markdown ### The limits of the transmitted filter frequencies
Нижняя_граница_пропускаемых_частот = 19 # @param {type:"slider", min:2, max:100, step:1}
Верхняя_граница_пропускаемых_частот = 45 # @param {type:"slider", min:2, max:100, step:1}
# @markdown ### The limits of the absorbed filter frequencies
Нижняя_граница_поглощамеых_частот = 12 # @param {type:"slider", min:2, max:100, step:1}
Верхняя_граница_поглощаемых_частот = 60 # @param {type:"slider", min:2, max:100, step:1}
# @markdown ### Peak Parameters
Пик_в_полосе_пропускания = 8 # @param {type:"slider", min:2, max:100, step:1}
Пик_в_полосе_поглощения = 39 # @param {type:"slider", min:2, max:100, step:1}
pB, pT = Нижняя_граница_пропускаемых_частот, Верхняя_граница_пропускаемых_частот
sB, sT = Нижняя_граница_поглощамеых_частот, Верхняя_граница_поглощаемых_частот
Rp = Пик_в_полосе_пропускания
Rs = Пик_в_полосе_поглощения
Wp = (pB, pT) ./ Fn;
Ws = (sB, sT) ./ Fn;
n,Ws = ellipord( Wp, Ws, Rp, Rs); # Let's find the filter order with the desired characteristics
designmethod = Elliptic( n, Rp, Rs ); # Creating an elliptical filter
responsetype = Bandpass( Rp, Rs); # And a bandpass filter
# Smoothed spectrum
TF = FFTW.fft( d.Measurement ) ./ L
TF1 = filt( digitalfilter(responsetype, designmethod; fs=Fn), TF );
plot(
plot( Fv, 20 .* log10.(abs.([TF[Iv] TF1[Iv]])), xscale=:log10, lw=[1 2] ),
plot( Fv, angle.([TF[Iv] TF1[Iv]]) .* 180/pi, xscale=:log10, lw=[1 2] ),
layout=(2,1)
)
plot!( ylabel=["Amplitude (dB)" "Phase (hail)"], leg=:false )
plot!( title=["LFCH systems" ""], xlabel=["" "Frequency (Hz)"] )
If you hide code, and set the option "Avtonapovnennya cell, if you change settings" button (the button <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" class="autoplay-icon"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.7874 6.88404C14.7826 6.89463 14.7777 6.90522 14.7732 6.916C14.7686 6.92717 14.765 6.93882 14.7614 6.95044C14.7556 6.96934 14.7498 6.98815 14.7399 7.00467C14.7202 7.03735 14.6944 7.06478 14.6685 7.09231C14.6612 7.10016 14.6538 7.10801 14.6465 7.116C14.6385 7.12435 14.6313 7.1335 14.6242 7.14265C14.6142 7.15542 14.6043 7.16818 14.5919 7.17867C14.5545 7.20916 14.5114 7.23036 14.4684 7.25148C14.4653 7.25299 14.4623 7.25449 14.4592 7.256C14.4501 7.26041 14.4415 7.26595 14.4328 7.27151C14.4206 7.2794 14.4082 7.28731 14.3945 7.292C14.3225 7.31867 14.2459 7.33333 14.1665 7.33333C14.1305 7.33333 14.0932 7.33067 14.0565 7.324L11.2232 6.854C10.8599 6.794 10.6145 6.45067 10.6745 6.08733C10.7345 5.72467 11.0772 5.476 11.4412 5.53933L12.5372 5.72067C11.7072 4.26333 10.1225 3.33333 8.37854 3.33333C6.40587 3.33333 4.66787 4.47867 3.94987 6.25C3.84521 6.51 3.59521 6.66667 3.33187 6.66667C3.24854 6.66667 3.16387 6.65133 3.08187 6.618C2.74054 6.48 2.57587 6.09133 2.71454 5.75C3.63721 3.472 5.86054 2 8.37854 2C10.6532 2 12.7205 3.24667 13.7639 5.192L14.0099 3.87667C14.0779 3.51533 14.4272 3.276 14.7879 3.34533C15.1499 3.41267 15.3885 3.76133 15.3205 4.12333L14.8212 6.79C14.8148 6.82401 14.8011 6.854 14.7874 6.88404ZM12.7142 9.74877C12.8522 9.40667 13.2415 9.24263 13.5822 9.38066C13.9235 9.5187 14.0882 9.90748 13.9495 10.2489C13.0269 12.5276 10.8035 14 8.28557 14C6.01092 14 3.94361 12.753 2.90028 10.8071L2.65362 12.1221C2.59428 12.4429 2.31429 12.6663 1.99896 12.6663C1.95829 12.6663 1.91696 12.6623 1.87562 12.6543C1.51363 12.5869 1.27563 12.2382 1.34363 11.8761L1.84296 9.20862C1.84802 9.18242 1.85925 9.15918 1.87052 9.13582C1.87704 9.12231 1.88358 9.10876 1.88896 9.09458C1.89423 9.0813 1.89883 9.06754 1.90344 9.05377C1.91341 9.02398 1.92342 8.99408 1.94029 8.96855C1.95013 8.95337 1.96299 8.94123 1.97594 8.929C1.98404 8.92135 1.99218 8.91367 1.99962 8.90519C2.00549 8.89839 2.0113 8.89155 2.0171 8.88471C2.04883 8.8473 2.08027 8.81022 2.12029 8.78316C2.12422 8.7803 2.12891 8.77859 2.13356 8.7769C2.13757 8.77544 2.14155 8.77399 2.14495 8.77182C2.20962 8.72981 2.27895 8.69713 2.35562 8.68046C2.36029 8.67938 2.36515 8.67966 2.37 8.67994C2.37415 8.68017 2.37829 8.68041 2.38229 8.6798C2.45562 8.66779 2.52962 8.66046 2.60762 8.67379L5.44093 9.1446C5.80426 9.20395 6.04959 9.54805 5.98959 9.91149C5.93559 10.2376 5.65293 10.469 5.33226 10.469C5.29626 10.469 5.25959 10.4663 5.22293 10.4596L4.12694 10.2783C4.95693 11.736 6.54158 12.6663 8.28557 12.6663C10.2582 12.6663 11.9962 11.5206 12.7142 9.74877Z" fill="#989DA0"></path></svg> to the left of the mask cells), it is possible to interactively adjust the filter parameters.
Conclusion
The toolkit of masks for code cells allows you to create your own interface for configuring algorithms. With its help, we can select the filter parameters that will appropriately affect the output signal of a certain link.
