示例#1
0
文件: eq.c 项目: OV2/RetroArch
static void create_filter(struct eq_data *eq, unsigned size_log2,
      struct eq_gain *gains, unsigned num_gains, double beta)
{
   int i;
   int half_block_size = eq->block_size >> 1;
   double window_mod = 1.0 / kaiser_window(0.0, beta);

   fft_t *fft = fft_new(size_log2);
   float *time_filter = (float*)calloc(eq->block_size * 2, sizeof(*time_filter));
   if (!fft || !time_filter)
      goto end;

   // Make sure bands are in correct order.
   qsort(gains, num_gains, sizeof(*gains), gains_cmp);

   // Compute desired filter response.
   generate_response(eq->filter, gains, num_gains, half_block_size);

   // Get equivalent time-domain filter.
   fft_process_inverse(fft, time_filter, eq->filter, 1);

   // ifftshift() to create the correct linear phase filter.
   // The filter response was designed with zero phase, which won't work unless we compensate
   // for the repeating property of the FFT here by flipping left and right blocks.
   for (i = 0; i < half_block_size; i++)
   {
      float tmp = time_filter[i + half_block_size];
      time_filter[i + half_block_size] = time_filter[i];
      time_filter[i] = tmp;
   }

   // Apply a window to smooth out the frequency repsonse.
   for (i = 0; i < (int)eq->block_size; i++)
   {
      // Kaiser window.
      double phase = (double)i / (eq->block_size - 1);
      phase = 2.0 * (phase - 0.5);
      time_filter[i] *= window_mod * kaiser_window(phase, beta);
   }

   // Debugging.
#if 0
   FILE *file = fopen("/tmp/test.txt", "w");
   if (file)
   {
      for (i = 0; i < (int)eq->block_size; i++)
         fprintf(file, "%.6f\n", time_filter[i]);
      fclose(file);
   }
#endif

   // Padded FFT to create our FFT filter.
   fft_process_forward(eq->fft, eq->filter, time_filter, 1);

end:
   fft_free(fft);
   free(time_filter);
}
示例#2
0
const FIRInstance *
ParamFIR::make(int sample_rate) const
{
  int i;

  /////////////////////////////////////////////////////////////////////////////
  // Normalize

  double norm_factor = norm? 1.0: 1.0 / sample_rate;
  double f1_ = f1 * norm_factor;
  double f2_ = f2 * norm_factor;
  double df_ = df * norm_factor;

  /////////////////////////////////////////////////////////////////////////////
  // Trivial cases

  if (f1_ < 0.0 || f2_ < 0.0 || df_ <= 0.0 || a < 0.0) return 0;
  if (a == 0.0) return new IdentityFIRInstance(sample_rate);

  switch (type)
  {
    case low_pass:
      if (f1_ >= 0.5) return new IdentityFIRInstance(sample_rate);
      if (f1_ == 0.0) return new GainFIRInstance(sample_rate, db2value(a));
      break;

    case high_pass:
      if (f1_ >= 0.5) return new GainFIRInstance(sample_rate, db2value(a));
      if (f1_ == 0.0) return new IdentityFIRInstance(sample_rate);
      break;

    case band_pass:
      if (f1_ >= 0.5) return new GainFIRInstance(sample_rate, db2value(a));
      if (f2_ == 0.0) return new GainFIRInstance(sample_rate, db2value(a));
      if (f1_ == 0.0 && f2_ >= 0.5) return new IdentityFIRInstance(sample_rate);
      break;

    case band_stop:
      if (f1_ >= 0.5) return new IdentityFIRInstance(sample_rate);
      if (f2_ == 0.0) return new IdentityFIRInstance(sample_rate);
      if (f1_ == 0.0 && f2_ >= 0.5) return new GainFIRInstance(sample_rate, db2value(a));
      break;

    default:
      return 0;
  }

  /////////////////////////////////////////////////////////////////////////////
  // Build the filter

  int n = kaiser_n(a, df_) | 1; // make odd (type 1 filter)
  int c = n / 2;

  DynamicFIRInstance *fir = new DynamicFIRInstance(sample_rate, n, c);
  double *filter = fir->buf;

  filter[0] = 1.0;
  double alpha = kaiser_alpha(a);
  switch (type)
  {
    case low_pass:
      for (i = 0; i < n; i++)
        filter[i] = (sample_t) (2 * f1_ * sinc((i - c) * 2 * M_PI * f1_) * kaiser_window(i - c, n, alpha));
      return fir;

    case high_pass:
      for (i = 0; i < n; i++)
        filter[i] = (sample_t) (-2 * f1_ * sinc((i - c) * 2 * M_PI * f1_) * kaiser_window(i - c, n, alpha));
      filter[c] = (sample_t) ((1 - 2 * f1_) * kaiser_window(0, n, alpha));
      return fir;

    case band_pass:
      for (i = 0; i < n; i++)
        filter[i] = (sample_t) ((2 * f2_ * sinc((i - c) * 2 * M_PI * f2_) - 2 * f1_ * sinc((i - c) * 2 * M_PI * f1_)) * kaiser_window(i - c, n, alpha));
      return fir;

    case band_stop:
      for (i = 0; i < n; i++)
        filter[i] = (sample_t) ((2 * f1_ * sinc((i - c) * 2 * M_PI * f1_) - 2 * f2_ * sinc((i - c) * 2 * M_PI * f2_)) * kaiser_window(i - c, n, alpha));
      filter[c] = (sample_t) ((2 * f1_ + 1 - 2 * f2_) * kaiser_window(0, n, alpha));
      return fir;
  };

  // never be here
  assert(false);
  delete filter;
  return 0;
}
示例#3
0
/**
 * @brief Default constructor. Set a start up configuration.
 * @param QWidget *parent - Link to parent Widget (default is null).
 */
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    try {
        timer = new QTimer(this);
        this->timer_for_record = new QTimer(this);

        connect(timer, SIGNAL(timeout()), this, SLOT(timerProc()));
        connect(timer_for_record, SIGNAL(timeout()), this, SLOT(stopRecordingSlot()));
        connect(this, SIGNAL(PlotSpectrumSignal()), this, SLOT(plotSpectrums()));
        connect(this, SIGNAL(fill_ftt_complex_Signal()), this, SLOT(from_ftt_to_complex()));
        connect(this, SIGNAL(post_filtering_Signal()), this, SLOT(post_filtering()));
        connect(this, SIGNAL(liftering_Signal()), this, SLOT(liftering()));
        connect(this, SIGNAL(kaiser_window_signal()), this, SLOT(kaiser_window()));

        ui->setupUi(this);
        ui->lineEdit->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        /* {
            ui->plot1->xAxis->setBasePen(QPen(Qt::white, 1));
            ui->plot1->yAxis->setBasePen(QPen(Qt::white, 1));
            ui->plot1->xAxis->setTickPen(QPen(Qt::white, 1));
            ui->plot1->yAxis->setTickPen(QPen(Qt::white, 1));
            ui->plot1->xAxis->setSubTickPen(QPen(Qt::white, 1));
            ui->plot1->yAxis->setSubTickPen(QPen(Qt::white, 1));
            ui->plot1->xAxis->setTickLabelColor(Qt::white);
            ui->plot1->yAxis->setTickLabelColor(Qt::white);
            ui->plot1->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
            ui->plot1->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
            ui->plot1->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
            ui->plot1->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
            ui->plot1->xAxis->grid()->setSubGridVisible(true);
            ui->plot1->yAxis->grid()->setSubGridVisible(true);
            ui->plot1->xAxis->grid()->setZeroLinePen(Qt::NoPen);
            ui->plot1->yAxis->grid()->setZeroLinePen(Qt::NoPen);
            ui->plot1->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
            ui->plot1->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);

            QLinearGradient plotGradient;
            plotGradient.setStart(0, 0);
            plotGradient.setFinalStop(0, 350);
            plotGradient.setColorAt(0, QColor(80, 80, 80));
            plotGradient.setColorAt(1, QColor(50, 50, 50));
            ui->plot1->setBackground(plotGradient);
            QLinearGradient axisRectGradient;
            axisRectGradient.setStart(0, 0);
            axisRectGradient.setFinalStop(0, 350);
            axisRectGradient.setColorAt(0, QColor(80, 80, 80));
            axisRectGradient.setColorAt(1, QColor(30, 30, 30));
            ui->plot1->axisRect()->setBackground(axisRectGradient);

            ui->plot1->rescaleAxes();
        }*/
        {
            ui->plot2->xAxis->setBasePen(QPen(Qt::white, 1));
            ui->plot2->yAxis->setBasePen(QPen(Qt::white, 1));
            ui->plot2->xAxis->setTickPen(QPen(Qt::white, 1));
            ui->plot2->yAxis->setTickPen(QPen(Qt::white, 1));
            ui->plot2->xAxis->setSubTickPen(QPen(Qt::white, 1));
            ui->plot2->yAxis->setSubTickPen(QPen(Qt::white, 1));
            ui->plot2->xAxis->setTickLabelColor(Qt::white);
            ui->plot2->yAxis->setTickLabelColor(Qt::white);
            ui->plot2->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
            ui->plot2->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
            ui->plot2->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
            ui->plot2->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
            ui->plot2->xAxis->grid()->setSubGridVisible(true);
            ui->plot2->yAxis->grid()->setSubGridVisible(true);
            ui->plot2->xAxis->grid()->setZeroLinePen(Qt::NoPen);
            ui->plot2->yAxis->grid()->setZeroLinePen(Qt::NoPen);
            ui->plot2->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
            ui->plot2->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
            QLinearGradient plotGradient;
            plotGradient.setStart(0, 0);
            plotGradient.setFinalStop(0, 350);
            plotGradient.setColorAt(0, QColor(80, 80, 80));
            plotGradient.setColorAt(1, QColor(50, 50, 50));
            ui->plot2->setBackground(plotGradient);
            QLinearGradient axisRectGradient;
            axisRectGradient.setStart(0, 0);
            axisRectGradient.setFinalStop(0, 350);
            axisRectGradient.setColorAt(0, QColor(80, 80, 80));
            axisRectGradient.setColorAt(1, QColor(30, 30, 30));
            ui->plot2->axisRect()->setBackground(axisRectGradient);

            ui->plot2->rescaleAxes();
        }
        QGraphicsDropShadowEffect *dse  = new QGraphicsDropShadowEffect(this);  // for a Record button
        QGraphicsDropShadowEffect *dse2 = new QGraphicsDropShadowEffect(this);  // for a Play button
        QGraphicsDropShadowEffect *dse3 = new QGraphicsDropShadowEffect(this);  // for a Save
        QGraphicsDropShadowEffect *dse4 = new QGraphicsDropShadowEffect(this);  // for a LineEdit

        dse ->  setBlurRadius(1);
        dse2->  setBlurRadius(1);
        dse3->  setBlurRadius(1);
        dse4->  setBlurRadius(1);

        dse ->  setOffset(QPoint(0,1));
        dse2->  setOffset(QPoint(0,1));
        dse3->  setOffset(QPoint(0,1));
        dse4->  setOffset(QPoint(0,1));

        QColor color;
        color.setRgb(1, 137, 255, 80);

        dse ->setColor(color);
        dse2->setColor(color);
        dse3->setColor(color);
        dse4->setColor(color);


        ui->pushButton  ->  setGraphicsEffect(dse);
        //ui->pushButton_2->  setGraphicsEffect(dse2);
        ui->pushButton_3->  setGraphicsEffect(dse3);
        ui->lineEdit    ->  setGraphicsEffect(dse4);

        this->fft       =   new std::float_t[2048];

        for (std::int32_t i = 0; i < 2048; i++) fft[i] = 0;
    }
    catch (...){
        QDEBUG("err");
    }
}