Clear Filters
Clear Filters

Separate and plot individual filter responses

58 views (last 30 days)
S
S on 4 Aug 2024 at 21:59
Commented: Umar on 5 Aug 2024 at 2:39
I am trying to create a plot similar to the attached image! I want to see the individual response each filter gives while displaying the center frequency of that filter on the y, adding together all of the responses at the top. I want to do this to display how much each filter is contributing and where it is most active. I was trying to use plot with CenterFreqs as my y but keep getting errors. This is what I have:
% 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
A=1;
input_signal = A*sin(2*pi*signal_freq*t);
% Get the center frequencies of the filters
CenterFreqs = getCenterFrequencies(gammaFiltBank);
% Process the input signal through the selected filters
output_signal = gammaFiltBank(input_signal);
Thank you for your time!
  1 Comment
Umar
Umar on 5 Aug 2024 at 2:39

Hi @S,

Please see updated code snippet below, please customize your modified code plots based on your preferences. Please see brief summary of code snippet below along with your modified code snippet.

% 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

A = 1;

input_signal = A * sin(2 * pi * signal_freq * t);

% Get the center frequencies of the filters

CenterFreqs = getCenterFrequencies(gammaFiltBank);

% Process the input signal through the selected filters

output_signal = gammaFiltBank(input_signal);

% Plot individual filter responses with center frequencies on the y-axis

figure;

for i = 1:numFilts

    subplot(numFilts, 1, i);
    plot(t, output_signal(:, i));
    title(['Filter Response - Center Freq: ', num2str(CenterFreqs(i)), ' Hz']);
    if i == numFilts
        xlabel('Time (s)');
    end
    ylabel('Amplitude');
    grid on;

end

% Sum up the responses of all filters

combined_response = sum(output_signal, 2);

% Plot the combined response along with individual filter responses

figure;

for i = 1:numFilts

    subplot(numFilts+1, 1, i);
    plot(t, output_signal(:, i), 'Color', [0.8 0.8 0.8]); % Individual filter responses
    ylabel(['Amplitude - Filter ', num2str(i)]);

end

subplot(numFilts+1, 1, numFilts+1);

plot(t, combined_response, 'k', 'LineWidth', 2); % Combined response

xlabel('Time (s)');

ylabel('Amplitude - Combined');

title('Combined Response and Individual Filter Responses');

% Display contribution of each filter to the combined response

figure;

bar(CenterFreqs, sum(output_signal, 1), 'b');

xlabel('Center Frequency (Hz)');

ylabel('Contribution to Combined Response');

title('Filter Contribution to Combined Response');

Please see attached.

Brief summary of code snippet

*The initial section of the code sets up parameters such as the sampling frequency fs, time vector t, number of filters numFilts, signal frequency signal_freq, frequency range range, and initializes the gammatone filter bank gammaFiltBank.

*An input sinusoidal signal is generated with a specific amplitude A and frequency signal_freq over the time vector t.

*The function getCenterFrequencies is used to extract the center frequencies of the gammatone filters in the bank.

*The input signal is processed through the gammatone filter bank using the gammaFiltBank function, resulting in output_signal, which contains the responses of each filter.

*A figure is created to display the responses of each filter individually over time. Each subplot represents a filter response with its corresponding center frequency.

*The responses of all filters are summed up to create a combined_response that represents the combined output of the filter bank.

*Another figure is generated to show the combined response along with the individual filter responses. The combined response is plotted in a separate subplot with a thicker line for clarity.

*A bar graph is plotted to illustrate the contribution of each filter to the combined response. The x-axis represents the center frequencies of the filters, and the y-axis shows the contribution values.

Sign in to comment.

Answers (0)

Categories

Find more on Audio Processing Algorithm Design 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!