Sample

Overview

This section introduces the function, realization process and details of each example of HPL module to help you quickly understand how to use the HPL module, and the introduction of the following samples are based on the board side running example. For sample to compile and run, please refer to the section Quick Start.

Sample list, dsp backend is used by default.

Sample NameDescription
fft_ifft_transformFFT IFFT Transform Sample

Fourier Transform Sample

The Fast Fourier Transform sample execution script is located in the directory ucp_tutorial/hpl/hpl_samples/script/01_fft_ifft_transform. This sample demonstrates how to filter images using Fast Fourier Transform and Inverse Fast Fourier Transform interfaces. Information may be interfered by various noises during the processes of collection,transmission, or processing. For some types of noise, dealing with them in the frequency domain is a more effective and convenient method. At this point, the relevant interfaces of Fourier transform can be used to remove these noises in the frequency domain, and then the filtered data can be restored to the time domain to achieve the purpose of denoising. For detailed implementation methods, please refer to the sample source code for practice comparison.

In this sample, directly executing the sample script can start the sample processing flow: reading noisy sine waves, fast fourier transformation, frequency domain filtering, inverse fast fourier transformation, and saving denoising data. The execution method is as follows:

sh run_fft_ifft_tranform.sh

In addition, you can obtain sample usage instructions by appending the -help command.

After the operator is executed, the data processing results will be saved in the hpl_samples/data directory. The output content of this sample is as follows:

hpl_samples └── data // Data directory ├── fft1d_input_f32.txt // Noisy data input by fft ├── fft1d_output_f32.txt // Noisy data output by fft ├── ifft1d_output_f32.txt // Denoised data output by ifft ├── input_image_2d.jpg // Input image by fft2d ├── input_image_f32.jpg // Noisy image input by fft └── output_image_f32.jpg // Denoised image output by ifft

The execution flow of the sample is shown below for 1D FFT:

HPL6_01Flow

As shown in the execution flow chart of the sample, the data processing process of this sample can be divided into the following four steps:

  1. Generate sine wave data and add noise. Save the generated data in a file as a floating point type and read the data directly in subsequent runs.

  2. Execute Fast Fourier Transform on the generated data.

  3. Construct a frequency domain filter to filter the transformed data.

  4. Execute Inverse Fast Fourier Transform on the filtering result to restore the sine wave data.

Constructing Noisy Data

The sine wave is used as the original data, and after adding noise, it becomes the input data, and the noise is removed in subsequent processing. The noise data is a sine wave with a frequency of 5HZ superimposed with Gaussian noise. The noise construction code is as follows:

for (int i = 0; i < data_length; ++i) { double t = i / sample_rate; signal.at<float>(i) = sin(2 * CV_PI * frequency * t); if (i % 3 == 0) { noise.at<float>(i) = rng.gaussian(0.2); } else { noise.at<float>(i) = 0.0f; } } cv::Mat noisy_signal = signal + noise;

Among them, the original sine wave image is:

HPL6FFT_input_raw

The image after adding noise is:

HPL6FFT_input_noise

Fourier Transform

The fourier transform interface converts data from the time domain to the frequency domain, allowing us to use some frequency domain methods to process the data. The HPL module provides a 1-dimensional fast Fourier transform interface. For details, please refer to Fast Fourier Transform. The specific code is as follows:

int32_t ret{HB_UCP_SUCCESS}; hbFFTParam param_fft{point_size, 0}; ret = hbFFT1D(nullptr, &dst, &src, &param_fft); FFT_LOGE_AND_RETURN_IF(ret != 0, HB_UCP_INVALID_ARGUMENT, "hbFFT1D failed, error code: {}", ret);

Among them, dst is the output data in the frequency domain, and src is the input data in the time domain, that is, noise data.

Frequency Domain Filtering

This step is to process the data in the frequency domain to achieve the effect of noise filtering. Here a filter is constructed and convolved with the frequency domain data. The specific implementation is as follows:

int32_t complex_filter(hbHPLImaginaryData &imaginary) { int32_t ret{HB_UCP_SUCCESS}; /**=============================================================== * copy imaginary data to mat * ===============================================================*/ cv::Mat complex_data(1, imaginary.dimensionSize[0], CV_32FC2); copy_imaginary_to_mat_32FC2<float32_t>(complex_data, imaginary); /**=============================================================== * create filter and apply * ===============================================================*/ int32_t data_length = imaginary.dimensionSize[0]; cv::Mat filter = cv::Mat::zeros(complex_data.size(), CV_32F); create_band_pass_filter(filter, 0, data_length / 220, data_length, data_length); cv::Mat filter_complex[] = {filter, cv::Mat::zeros(filter.size(), CV_32F)}; cv::Mat complex_filter, filtered_data; merge(filter_complex, 2, complex_filter); mulSpectrums(complex_data, complex_filter, filtered_data, 0, true); /**=============================================================== * copy mat to imaginary data * ===============================================================*/ copy_mat_32FC2_to_imaginary<float32_t>(imaginary, filtered_data); return ret; }

In the code, the complex number structure data is converted into the data type of cv::Mat, and the frequency domain filter is created and applied through the relevant interface provided by OpenCV. Finally, the filtering result is converted into an complex number structure.

Inverse Fourier Transform

The Inverse Fourier Transform transforms data from the frequency domain to the time domain, making the display and storage of data more consistent with daily cognition. The HPL module provides a 1-dimensional inverse fast Fourier transform interface. For details, please refer to Inverse Fast Fourier Transform. The specific code is as follows:

int32_t ret{HB_UCP_SUCCESS}; hbIFFTParam param_ifft{point_size, 0}; ret = hbIFFT1D(nullptr, &dst, &src, &param_ifft); FFT_LOGE_AND_RETURN_IF(ret != 0, HB_UCP_INVALID_ARGUMENT, "hbIFFT1D failed, error code: {}", ret);

Among them, dst is the output data in the time domain, and src is the input data in the frequency domain.

The image after removing noise is:

HPL6FFT_output