フィルターのクリア

Find the closest freq in a filterbank

28 ビュー (過去 30 日間)
S
S 2024 年 7 月 15 日 17:14
コメント済み: S 2024 年 7 月 17 日 22:05
I am using a filter bank to run different freq signals through. I am trying to make it more efficient (to not have to use all the filters every time) so I am trying to find a way to specify in my code to only use the 3 filters with the closest center frequency to the input. Right now I am only able to input the filter numbers I want manually (with filter_number). I am not sure how I would change this. I was trying to use the 'find' function but was only able to make it work using freq not filter numbers. Like this:
indf = find(CenterFreqs<signal_freq)
How can I chnage something like the above line to instead use filter number in my code below?Thank you for your time!
%Parameters
fs = 16e3;
t = 0:(1/fs):0.03;
t = t(:); % ensure column vector
numFilts = 32;
filter_number = 10;
range = [50 8000];
gammaFiltBank = gammatoneFilterBank(range,numFilts,fs); % set fs explicity
input_signal = sin(2*pi*100*t)
output_signal = gammaFiltBank(input_signal);
figure %1
stem(t,output_signal(:,filter_number));
title(sprintf('Output of Filter %d',filter_number))
figure
impulse_input = 0*t;
impulse_input(1) = 1;
reset(gammaFiltBank); % IMPORTANT!
yimp = gammaFiltBank(impulse_input);
%Add together outputs of specific filters
filter_number2=12;
Add1=yimp(:,filter_number);
Add2=yimp(:,filter_number2);
Total=Add1 + Add2;
stem(t,Total)
title(sprintf('Impulse of %d plus %d',filter_number,filter_number2))
  2 件のコメント
Umar
Umar 2024 年 7 月 15 日 18:00
編集済み: Walter Roberson 2024 年 7 月 15 日 19:52
Hi S,
Obtain the frequency response of the filter using the 'freqz' function in your code. For more information on this function, please refer to
% Obtain frequency response
[h, f] = freqz(output_signal(:, filter_number));
Then apply the obtained response to filter the output signal using the 'filter' function.
% Apply frequency filtering
filtered_output = filter(h, 1, output_signal);
This will allow you to achieve frequency-based filtering for your specific filter number.
Please let me know if you have any further questions.
Umar
Umar 2024 年 7 月 16 日 0:20
Thanks Walter for your help with making my code cleaner.

サインインしてコメントする。

回答 (1 件)

Milan Bansal
Milan Bansal 2024 年 7 月 15 日 18:18
Hi S,
I understand that you wish to use the 3 filters from your filter bank which have their center frequency closest to the frequency of the input signal.
Please refer to the following steps to achive this:
  1. Get the center frequencies of all the filters in the filter bank using the getCenterFrequencies function.
  2. Calculate the absolute difference of center frequency and center frequencies from 1st step.
  3. Get the sorted indices of the difference and select the 1st 3 indices. These are the filter numbers which have closest center frequencies.
  4. Use these filter numbers to get the desired output.
Please refer to the following code snippet to modify your code:
% Parameters
fs = 16e3;
t = 0:(1/fs):0.03;
t = t(:); % ensure column vector
numFilts = 32;
signal_freq = 100; % frequency of input signal
range = [50 8000];
gammaFiltBank = gammatoneFilterBank(range, numFilts, fs); % set fs explicitly
% Generate input signal
input_signal = sin(2*pi*signal_freq*t);
% Get the center frequencies of the filters
CenterFreqs = getCenterFrequencies(gammaFiltBank);
% Find the 3 filters with the closest center frequencies to the input signal's frequency
[~, sorted_indices] = sort(abs(CenterFreqs - signal_freq));
closest_filters = sorted_indices(1:3);
% Process the input signal through the selected filters
output_signal = gammaFiltBank(input_signal);
figure
stem(t, output_signal(:, closest_filters(1)));
title(sprintf('Output of Closest Filter %d', closest_filters(1)))
figure
stem(t, output_signal(:, closest_filters(2)));
title(sprintf('Output of Closest Filter %d', closest_filters(2)))
figure
stem(t, output_signal(:, closest_filters(3)));
title(sprintf('Output of Closest Filter %d', closest_filters(3)))
% Generate impulse response and add the outputs of the selected filters
impulse_input = 0*t;
impulse_input(1) = 1;
reset(gammaFiltBank); % IMPORTANT!
yimp = gammaFiltBank(impulse_input);
% Add together outputs of specific filters
Add1 = yimp(:, closest_filters(1));
Add2 = yimp(:, closest_filters(2));
Add3 = yimp(:, closest_filters(3));
Total = Add1 + Add2 + Add3;
figure
stem(t, Total)
title(sprintf('Impulse of Closest Filters %d, %d, and %d', closest_filters(1), closest_filters(2), closest_filters(3)))
Please refer to the following documentation link to learn more about getCenterFrequencies function.
Hope this helps!
  1 件のコメント
S
S 2024 年 7 月 17 日 22:05
@Milan Bansal Thank you! Just to be sure, this line:
closest_filters = sorted_indices(1:3);
is not just giving me filter 1,2,and 3's response, it is giving the 3 filters with the center frequencies closest to my input (100 in this case) so it could technically be filter 5,6, and 7 for ex. even though it is labeled 1-3. Right? I just want to make sure I am understanding correctly

サインインしてコメントする。

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by