Find the closest freq in a filterbank
7 Comments
Hi @S,
After reading your comments second time, “ This is using filter number as an input, where I do not want it to necessarily use the same filter every time.” It helped me to clarify my thoughts little better, so to achieve frequency-based filtering without being constrained to a fixed filter number, you can modify the code to calculate the closest filter number for each input frequency. Instead of using a fixed filter_number, you can iterate over all available filters and choose the one that minimizes the difference between its frequency response and the desired input frequency. I have modified code above to demonstrate this approach:
% Generate or load the 'output_signal' variable output_signal = your_output_signal_generation_function(); % Replace with actual signal generation code
% Define input frequency input_frequency = 0.3; % Example input frequency
% Initialize variables for closest filter selection min_difference = Inf; closest_filter_number = 1;
% Iterate over all filters to find the closest one
for filter_number = 1:num_filters % num_filters is the total number of filters
% Obtain frequency response for the current filter [h, f] = freqz(output_signal(:, filter_number));
% Calculate the difference between current filter response and input frequency difference = abs(f - input_frequency); % Update closest filter if the current one is closer if min(difference) < min_difference min_difference = min(difference); closest_filter_number = filter_number; end end
% Obtain the frequency response for the closest filter [h_closest, ~] = freqz(output_signal(:, closest_filter_number));
% Apply frequency filtering using the closest filter filtered_output_closest = filter(h_closest, 1, output_signal);
Hope, this will help to achieve your task now, let me know if this is what you’re looking for.
Answers (2)
- Get the center frequencies of all the filters in the filter bank using the getCenterFrequencies function.
- Calculate the absolute difference of center frequency and center frequencies from 1st step.
- Get the sorted indices of the difference and select the 1st 3 indices. These are the filter numbers which have closest center frequencies.
- Use these filter numbers to get the desired output.
2 Comments
0 votes
Hi @S,
To achieve frequency-based filtering in MATLAB, where the filter selection is not fixed but rather determined by the proximity of the filter's frequency response to the desired input frequency, first, you need to know that each filter has a specific frequency response that can be obtained using the freqz function. This function returns the frequency response of a filter given its coefficients. So, instead of using a predetermined filter number, iterate through all available filters. For each filter, compute its frequency response and determine how close it is to the desired input frequency. Afterwards, maintain a variable to track the minimum difference between the input frequency and the frequencies of the filters. The filter that yields the smallest difference will be selected for filtering the output signal. Once the closest filter is identified, we will apply it to the output signal using the filter function. Here is a complete example that demonstrates it.
% Generate or load the 'output_signal' variable output_signal = your_output_signal_generation_function(); % Replace with actual signal generation code
% Define input frequency input_frequency = 0.3; % Example input frequency
% Initialize variables for closest filter selection num_filters = size(output_signal, 2); % Assuming output_signal has filters as columns min_difference = Inf; closest_filter_number = 1;
% Iterate over all filters to find the closest one for filter_number = 1:num_filters % Obtain frequency response for the current filter [h, f] = freqz(output_signal(:, filter_number));
% Calculate the difference between current filter response and input
frequency
difference = abs(f - input_frequency); % Update closest filter if the current one is closer
if min(difference) < min_difference
min_difference = min(difference);
closest_filter_number = filter_number;
end
end% Obtain the frequency response for the closest filter [h_closest, ~] = freqz(output_signal(:, closest_filter_number));
% Apply frequency filtering using the closest filter filtered_output_closest = filter(h_closest, 1, output_signal);
% Display the results disp(['The closest filter number is: ', num2str(closest_filter_number)]);
Integrate this code into your existing code
So, at this point, integrate this code snippet into your existing MATLAB script. Simply place it below your current filtering logic. Make sure that the output_signal variable is correctly defined and that the number of filters is accurately represented in the num_filters variable.
This approach should allow you now for a flexible and dynamic selection of filters based on the input frequency, enhancing the adaptability of your filtering process.
Should you have any further questions or require additional modifications, please feel free to reach out.
Categories
Find more on Frequency Transformations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


