code: def plot_filter_kernels(
      filters: torch.Tensor, sample_rate: int, args
  ):
      """Plot the Sinc filter kernels.
  
      Args:
          filters (torch.Tensor): Filter parameters.
          sample_rate (int): Sample rate of Signal.
          args (dict): Dictionary with output options.
      """
      from espnet2.layers.sinc_conv import SincConv
  
      print(
          "When plotting filter kernels, make sure the script has the"
          " correct SincConv settings (currently hard-coded)."
      )
      convs = SincConv(1, 128, 101)
  
      # unlearned
      convs._create_filters(convs.f.device)
      pre_kernels = convs.sinc_filters.detach().numpy()
  
      pre_filters = convs.f.detach().numpy()
      f_mins = np.abs(pre_filters[:, 0])
      f_maxs = np.abs(pre_filters[:, 0]) + np.abs(
          pre_filters[:, 1] - pre_filters[:, 0]
      )
      F_mins, F_maxs = f_mins * sample_rate, f_maxs * sample_rate
      pre_F_mins, pre_F_maxs = (
          np.round(F_mins).astype(np.int64),
          np.round(F_maxs).astype(np.int64),
      )
  
      # learned
      convs.f = torch.nn.Parameter(torch.Tensor(filters))
      convs._create_filters(convs.f.device)
      kernels = convs.sinc_filters.detach().numpy()
  
      f_mins = np.abs(filters[:, 0])
      f_maxs = np.abs(filters[:, 0]) + np.abs(
          filters[:, 1] - filters[:, 0]
      )
      F_mins, F_maxs = f_mins * sample_rate, f_maxs * sample_rate
      F_mins, F_maxs = (
          np.round(F_mins).astype(np.int64),
          np.round(F_maxs).astype(np.int64),
      )
      F_mins, F_maxs = (
          np.clip(F_mins, 0, sample_rate / 2.0),
          np.clip(F_maxs, 0, sample_rate / 2.0),
      )
  
      x_f = np.linspace(0.0, np.max(F_maxs), int(np.max(F_maxs)) + 1)
      x = np.arange(kernels.shape[2])
      if args.all:
          for i in range(len(kernels)):
              pre_kernel = pre_kernels[i][0]
              plt.clf()
              plt.xticks([])
              plt.yticks([])
              plt.plot(x, pre_kernel)
              img_name = "filter_pre_kernel_%s.%s" % (
                  str(i).zfill(2),
                  args.filetype,
              )
              img_path = str(args.out_folder / img_name)
              plt.savefig(img_path, bbox_inches="tight")
              print("Plotted %s" % img_path)
  
              kernel = kernels[i][0]
              plt.clf()
              plt.xticks([])
              plt.yticks([])
              plt.plot(x, kernel)
              img_name = "filter_kernel_%s.%s" % (
                  str(i).zfill(2),
                  args.filetype,
              )
              img_path = str(args.out_folder / img_name)
              plt.savefig(img_path, bbox_inches="tight")
              print("Plotted %s" % img_path)
  
              plt.clf()
              plt.xlabel("kernel index")
              plt.plot(x, kernel)
              plt.plot(x, pre_kernel, "--", alpha=0.5)
              img_name = "filter_kernel_both_%s.%s" % (
                  str(i).zfill(2),
                  args.filetype,
              )
              img_path = str(args.out_folder / img_name)
              plt.savefig(img_path, bbox_inches="tight")
              print("Plotted %s" % img_path)
  
              y = np.zeros_like(x_f)
              y[F_mins[i] : F_maxs[i]] = 1.0
              plt.clf()
              plt.plot(x_f, y)
              img_name = "filter_freq_%s.%s" % (
                  str(i).zfill(2),
                  args.filetype,
              )
              img_path = str(args.out_folder / img_name)
              plt.savefig(img_path, bbox_inches="tight")
              print("Plotted %s" % img_path)
  
              pre_y = np.zeros_like(x_f)
              pre_y[pre_F_mins[i] : pre_F_maxs[i]] = 1.0
              plt.clf()
              plt.plot(x_f, y)
              plt.plot(x_f, pre_y)
              img_name = "filter_freq_both_%s.%s" % (
                  str(i).zfill(2),
                  args.filetype,
              )
              img_path = args.out_folder / img_name
              plt.savefig(img_path, bbox_inches="tight")
              print("Plotted %s" % img_path)
  
      plt.clf()
      filters = [32, 71, 113, 126]
      fig, axs = plt.subplots(2, 2, sharex=True, sharey="row")
  
      axs[0, 0].plot(x, kernels[filters[0]][0])
      axs[0, 0].plot(x, pre_kernels[filters[0]][0], "--", alpha=0.5)
      axs[0, 1].plot(x, kernels[filters[1]][0])
      axs[0, 1].plot(x, pre_kernels[filters[1]][0], "--", alpha=0.5)
      axs[1, 0].plot(x, kernels[filters[2]][0])
      axs[1, 0].plot(x, pre_kernels[filters[2]][0], "--", alpha=0.5)
      axs[1, 1].plot(x, kernels[filters[3]][0])
      axs[1, 1].plot(x, pre_kernels[filters[3]][0], "--", alpha=0.5)
  
      img_name = "filter_kernel_ensemble2.%s" % (args.filetype)
      img_path = str(args.out_folder / img_name)
      plt.savefig(img_path, bbox_inches="tight")
      plt.close(fig)
      print("Plotted %s" % img_path)
  
file_path: espnet/egs2/accented_french_openslr57/asr1/pyscripts/utils/plot_sinc_filters.py
imports: import argparse
  import sys
  from pathlib import Path
  import matplotlib.pyplot
  import numpy
  import torch
label_desc: Use image node_id 4 for calling a global function with example usage: plot_filter_kernels(filters, sample_rate, args) without return types