#! /bin/bash # Last edited on 2024-01-04 22:16:32 by stolfi # Reads a filter's gain function from a file # and writes a plot of of it to a file with same name # except with ".txt" replaced by "_log{YLOG}_vty{VALTYPE}.png". # # Usage: # # nmeeg_plot_filter.sh \ # {SHOW} \ # {FILTER_FILE} \ # {FTYPE} {YLOG} {VALTYPE} \ # {HSIZE} {VSIZE} # # where # # {SHOW} "SHOW" to display, "NOSHOW" not to. # {FILTER_FILE} is the file with filter gains. # {FTYPE} is the filter type, for title purposes. # {YLOG} is 1 if the transfer function is real non-negative (phase-preserving), 0 otherwise. # {VALTYPE} is the type of transfer function: 0 = real non-negative, 1 = signed real, 2 = complex. # {HSIZE} is the plot's total horizontal size in pixels. # {VSIZE} is the plot's total vertical size in pixels. # # Reads the filter coefficients from standard input, which is supposed to contain # the transfer fucntion of a discrete Fourier filter, one frequency per line, with # fields "{KFREQ} {FREQ} {FREAL} {FIMAG} {HARC} {HARS}" # plus initial lines "# {key} = {value}" that define the # number of frames {nf}, the sampling frequency {fsmp}, # and the frequency parameters {flo0,flo1,fhi1,fhi0}. echo "$0 $@" 1>&2 show="$1"; shift dfile="$1"; shift ftype="$1"; shift ylog="$1"; shift valtype="$1"; shift hsize="$1"; shift vsize="$1"; shift ls -l ${dfile} pfile="${dfile/.txt/}_log${ylog}_vty${valtype}.png" rm -f ${pfile} echo "extracting filter parameters..." 1>&2 nf=`head -n 10 ${dfile} | egrep -e '^ *[#] *nf *[=]' | sed -e 's:^.*[=] *::g'` fsmp=`head -n 10 ${dfile} | egrep -e '^ *[#] *fsmp *[=]' | sed -e 's:^.*[=] *::g'` flo0=`head -n 10 ${dfile} | egrep -e '^ *[#] *flo0 *[=]' | sed -e 's:^.*[=] *::g'` flo1=`head -n 10 ${dfile} | egrep -e '^ *[#] *flo1 *[=]' | sed -e 's:^.*[=] *::g'` fhi1=`head -n 10 ${dfile} | egrep -e '^ *[#] *fhi1 *[=]' | sed -e 's:^.*[=] *::g'` fhi0=`head -n 10 ${dfile} | egrep -e '^ *[#] *fhi0 *[=]' | sed -e 's:^.*[=] *::g'` echo "computing Nyquist freq..." 1>&2 fnyq=`echo "0.5*${fsmp}" | bc -lq` # Nyquist frequency. echo "fsmp=${fsmp} flo0=${flo0} flo1=${flo1} fhi1=${fhi1} fhi0=${fhi0} fnyq=${fnyq}" 1>&2 tmp="/tmp/$$" # Create file with key frequencies: ifile="${tmp}_i.txt" rm -f ${ifile} echo "${flo0} -100.0" >> ${ifile} echo "${flo0} +100.0" >> ${ifile} echo "${flo1} -100.0" >> ${ifile} echo "${flo1} +100.0" >> ${ifile} echo "${fhi1} -100.0" >> ${ifile} echo "${fhi1} +100.0" >> ${ifile} echo "${fhi0} -100.0" >> ${ifile} echo "${fhi0} +100.0" >> ${ifile} echo "${fnyq} -100.0" >> ${ifile} echo "${fnyq} +100.0" >> ${ifile} if [[ ${ylog} -ne 0 ]]; then ylogmax=9 theytics=( \ "'-10'" -11.000 0 , \ "''" -10.699 1 , \ "'-1'" -10.000 0 , \ "''" -9.699 1 , \ "'-0.1'" -9.000 0 , \ "''" -8.699 1 , \ "'-0.01'" -8.000 0 , \ "''" -7.699 1 , \ "'-0.001'" -7.000 0 , \ "''" -6.699 1 , \ "'-0.0001'" -6.000 0 , \ "''" -5.699 1 , \ "'-1.0e-5'" -5.000 0 , \ "''" -4.699 1 , \ "'-1.0e-6'" -4.000 0 , \ "''" -3.699 1 , \ "'-1.0e-7'" -3.000 0 , \ "''" -2.699 1 , \ "'-1.0e-8'" -2.000 0 , \ "''" -1.699 1 , \ "'-1.0e-9'" -1.000 0 , \ "''" -0.699 1 , \ "'0'" 0.000 0 , \ "''" 0.699 1 , \ "'+1.0e+9'" 1.000 0 , \ "''" 1.699 1 , \ "'+1.0e+8'" 2.000 0 , \ "''" 2.699 1 , \ "'+1.0e+7'" 3.000 0 , \ "''" 3.699 1 , \ "'+1.0e+6'" 4.000 0 , \ "''" 4.699 1 , \ "'+1.0e+5'" 5.000 0 , \ "''" 5.699 1 , \ "'+0.0001'" 6.000 0 , \ "''" 6.699 1 , \ "'+0.001'" 7.000 0 , \ "''" 7.699 1 , \ "'+0.01'" 8.000 0 , \ "''" 8.699 1 , \ "'+0.1'" 9.000 0 , \ "''" 9.699 1 , \ "'+1'" 10.000 0 , \ "''" 10.699 1 , \ "'+10'" 11.000 0 \ ) else theytics=( \ "'-1.7'" -1.70 0 , \ "''" -1.65 1 , \ "'-1.6'" -1.60 0 , \ "''" -1.55 1 , \ "'-1.5'" -1.50 0 , \ "''" -1.45 1 , \ "'-1.4'" -1.40 0 , \ "''" -1.35 1 , \ "'-1.3'" -1.30 0 , \ "''" -1.25 1 , \ "'-1.2'" -1.20 0 , \ "''" -1.15 1 , \ "'-1.1'" -1.10 0 , \ "''" -1.05 1 , \ "'-1.0'" -1.00 0 , \ "''" -0.95 1 , \ "'-0.9'" -0.90 0 , \ "''" -0.85 1 , \ "'-0.8'" -0.80 0 , \ "''" -0.75 1 , \ "'-0.7'" -0.70 0 , \ "''" -0.65 1 , \ "'-0.6'" -0.60 0 , \ "''" -0.55 1 , \ "'-0.5'" -0.50 0 , \ "''" -0.45 1 , \ "'-0.4'" -0.40 0 , \ "''" -0.35 1 , \ "'-0.3'" -0.30 0 , \ "''" -0.25 1 , \ "'-0.2'" -0.20 0 , \ "''" -0.15 1 , \ "'-0.1'" -0.10 0 , \ "''" -0.05 1 , \ "'0.0'" 0.00 0 , \ "''" +0.05 1 , \ "'0.1'" +0.10 0 , \ "''" +0.15 1 , \ "'0.2'" +0.20 0 , \ "''" +0.25 1 , \ "'0.3'" +0.30 0 , \ "''" +0.35 1 , \ "'0.4'" +0.40 0 , \ "''" +0.45 1 , \ "'0.5'" +0.50 0 , \ "''" +0.55 1 , \ "'0.6'" +0.60 0 , \ "''" +0.65 1 , \ "'0.7'" +0.70 0 , \ "''" +0.75 1 , \ "'0.8'" +0.80 0 , \ "''" +0.85 1 , \ "'0.9'" +0.90 0 , \ "''" +0.95 1 , \ "'1.0'" +1.00 0 , \ "''" +1.05 1 , \ "'1.1'" +1.10 0 , \ "''" +1.15 1 , \ "'1.2'" +1.20 0 , \ "''" +1.25 1 , \ "'1.3'" +1.30 0 , \ "''" +1.35 1 , \ "'1.4'" +1.40 0 , \ "''" +1.45 1 , \ "'1.5'" +1.50 0 , \ "''" +1.55 1 , \ "'1.6'" +1.60 0 , \ "''" +1.65 1 , \ "'1.7'" +1.70 0 \ ) fi title=`printf "Filter: %s %.3f %.3f %.3f %.3f" "${ftype}" "${flo0}" "${flo1}" "${fhi1}" "${fhi0}"` tfile="${tmp}.png" export GDFONTPATH=.:${HOME}/ttf gnuplot < 120 ? 20 : 10) # Step in second plot half - divisor of fbreak! set grid xtics set grid ytics yval(k) = ymap(column(k)) ymod(kr,ki) = ymap(sqrt(column(kr)**2 + column(ki)**2)) if (ylog) { ylogmax = ${ylogmax} safelog(y) = log(y)/log(10) + ylogmax + 1 hidesmall(s) = (s < 1 ? 0/0 : s) ymap(y) = (y == 0 ? 0 : (y < 0 ? -1 : +1)*hidesmall(safelog(abs(y)))) if (valtype == 0) { set yrange [-0.02:(+1.2*ylogmax)] } else { set yrange [(-1.2*ylogmax):(+1.2*ylogmax)] } } else { ymap(y) = (y < -50 ? 0/0 : y) if ( valtype == 0 ) { set yrange [-0.02:+1.6] } else { set yrange [-1.6:+1.6] } } set multiplot layout 1,2 title "${title}" # ---------------------------------------------------------------------- # First half: 0 to 35 Hz set origin 0.000, 0.000 set size 0.600,0.950 unset key set xrange [-0.20:35.2] set xtics out 0,1,35 set mxtics 5 set grid xtics lt 1 lw 3 lc rgb '#dddddd', lt 1 lw 1.5 lc rgb '#dddddd' set grid mxtics unset y2tics unset mytics set ytics ( ${theytics[@]} ) set grid ytics lt 1 lw 3 lc rgb '#dddddd', lt 1 lw 1.5 lc rgb '#dddddd' set grid mytics if ((valtype == 0) || (valtype == 1)) { plot \ "${ifile}" using 1:2 notitle with impulses lw 6 lc rgb '#ffccaa', \ "${dfile}" using 2:(yval(3)) title "W" with linespoints pt 7 ps 1 lw 3 lc rgb '#0022ff' } else { plot \ "${ifile}" using 1:2 notitle with impulses lw 6 lc rgb '#ffccaa', \ "${dfile}" using 2:(yval(3)) title "re(W)" with linespoints pt 7 ps 1 lw 3 lc rgb '#0022ff', \ "${dfile}" using 2:(yval(4)) title "im(W)" with linespoints pt 7 ps 1 lw 3 lc rgb '#ff0033', \ "${dfile}" using 2:(ymod(3,4)) title "|W|" with linespoints pt 7 ps 1 lw 3 lc rgb '#008833' } # ---------------------------------------------------------------------- # Second half: 0 to {fmax} Hz set origin 0.580, 0.000 set size 0.400,0.950 set key center rmargin set xrange [-(0.01*fmax):(1.01*fmax)] set xtics out 0,20 set mxtics 4 set grid xtics lt 1 lw 3 lc rgb '#dddddd', lt 1 lw 1.5 lc rgb '#dddddd' set grid mxtics set ytics out right nomirror format "" unset ytics unset mytics set y2tics ( ${theytics[@]} ) set grid y2tics lt 1 lw 3 lc rgb '#dddddd', lt 1 lw 1.5 lc rgb '#dddddd' set grid my2tics if ((valtype == 0) || (valtype == 1)) { plot \ "${ifile}" using 1:2 notitle with impulses lw 6 lc rgb '#ffccaa', \ "${dfile}" using 2:(yval(3)) title "W" with linespoints pt 7 ps 1 lw 3 lc rgb '#0022ff' } else { plot \ "${ifile}" using 1:2 notitle with impulses lw 6 lc rgb '#ffccaa', \ "${dfile}" using 2:(yval(3)) title "re(W)" with linespoints pt 7 ps 1 lw 3 lc rgb '#0022ff', \ "${dfile}" using 2:(yval(4)) title "im(W)" with linespoints pt 7 ps 1 lw 3 lc rgb '#ff0033', \ "${dfile}" using 2:(ymod(3,4)) title "|W|" with linespoints pt 7 ps 1 lw 3 lc rgb '#008833' } # ---------------------------------------------------------------------- unset multiplot quit EOF rm -f ${ifile} if [[ -s ${tfile} ]]; then convert ${tfile} -resize '50%' ${pfile} rm ${tfile} if [[ -s ${pfile} ]]; then if [[ "/${SHOW}" == "/SHOW" ]]; then display -title "${dfile/.txt/}" ${pfile} fi else echo "** resize failed" 1>&2; exit 1 fi else echo "** gnuplot failed" 1>&2; exit 1 fi