Separate and plot individual filter responses

3 Ansichten (letzte 30 Tage)
S
S am 4 Aug. 2024
Kommentiert: Umar am 6 Sep. 2024
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!
  4 Kommentare
Umar
Umar am 4 Sep. 2024

Hi @S,

To address your equest, I have to focus on two main tasks:

1. Modifying the existing code to allow for user input on which outputs to display. 2. Providing a more streamlined and customizable plotting process based on user specifications.

Here’s an adapted version of the original code snippet that includes user input functionality for selecting the outputs:

% Parameters
fs = 16e3; % Sampling frequency
t = 0:(1/fs):0.03; % Time vector
t = t(:); % Ensure column vector
numFilts = 32; % Number of filters
signal_freq = 100; % Frequency of input signal
range = [50 8000]; % Frequency range
% Create gammatone filter bank
gammaFiltBank = gammatoneFilterBank(range, numFilts, fs); % Set fs 
explicitly
% Generate input signal
A = 1;
input_signal = A * sin(2 * pi * signal_freq * t);
% Get center frequencies of the filters
CenterFreqs = getCenterFrequencies(gammaFiltBank);
% Process the input signal through the selected filters
output_signal = gammaFiltBank(input_signal);
% Ask the user to specify which outputs to display
disp('Select the outputs you want to display:');
disp('1: Individual Filter Responses');
disp('2: Combined Response');
disp('3: Filter Contribution to Combined Response');
userChoice = input('Enter your choice (e.g., [1, 2]): ');
% Plot individual filter responses if selected
if ismember(1, userChoice)
  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
end
% Plot combined response if selected
if ismember(2, userChoice)
  combined_response = sum(output_signal, 2);
  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');
end
% Plot filter contributions if selected
if ismember(3, userChoice)
  figure;
  bar(CenterFreqs, sum(output_signal, 1), 'b');
  xlabel('Center Frequency (Hz)');
  ylabel('Contribution to Combined Response');
  title('Filter Contribution to Combined Response');
end

Summary of modifications made to the code

User Input for Output Selection: The script prompts the user to enter their desired output choices. The user can input an array (e.g., [1, 2]) to specify which plots to generate.

Conditional Plotting: Each plotting section is wrapped in an if statement that checks whether the corresponding option was selected by the user.

By implementing these modifications, it will help create a more tailored and efficient workflow when analyzing and visualizing their signal processing results.

Umar
Umar am 6 Sep. 2024
Hi @S,
Please let me know if you are looking for further assistance.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Umar
Umar am 4 Sep. 2024

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; % Sampling frequency set to 16 kHz
t = 0:(1/fs):0.03; % Time vector from 0 to 0.03 seconds with increments of 1/fs
t = t(:); % Ensure the time vector is a column vector for consistent matrix   operations
numFilts = 32; % Number of filters in the filter bank
signal_freq = 100; % Frequency of the input signal set to 100 Hz
range = [50 8000]; % Frequency range for the filter bank from 50 Hz to 8000 Hz
% Create a Gammatone filter bank with specified range, number of filters, and   sampling frequency
gammaFiltBank = gammatoneFilterBank(range, numFilts, fs); 
% Generate input signal
A = 1; % Amplitude of the input signal
input_signal = A * sin(2 * pi * signal_freq * t); % Generate a sinusoidal input   signal based on the specified frequency and amplitude
% Get the center frequencies of the filters
CenterFreqs = getCenterFrequencies(gammaFiltBank); % Retrieve the center   frequencies of the filters in the filter bank
% Process the input signal through the selected filters
output_signal = gammaFiltBank(input_signal); % Apply the filter bank to the   input signal to obtain the output signal
% Plot individual filter responses with center frequencies on the y-axis
figure; % Create a new figure for plotting
for i = 1:numFilts % Loop through each filter
  subplot(numFilts, 1, i); % Create a subplot for each filter response
  plot(t, output_signal(:, i)); % Plot the output signal for the current filter
  title(['Filter Response - Center Freq: ', num2str(CenterFreqs(i)), ' Hz']); %   Set the title to indicate the center frequency of the filter
  if i == numFilts % Check if it is the last filter
      xlabel('Time (s)'); % Label the x-axis for the last subplot
  end
  ylabel('Amplitude'); % Label the y-axis for each subplot
  grid on; % Enable grid for better visualization
end
% Sum up the responses of all filters
combined_response = sum(output_signal, 2); % Sum the output signals across all   filters to get the combined response
% Plot the combined response along with individual filter responses
figure; % Create a new figure for the combined response plot
for i = 1:numFilts % Loop through each filter
  subplot(numFilts+1, 1, i); % Create a subplot for each individual filter   response
  plot(t, output_signal(:, i), 'Color', [0.8 0.8 0.8]); % Plot the individual   filter responses in light gray
  ylabel(['Amplitude - Filter ', num2str(i)]); % Label the y-axis for each   subplot with filter number
end
subplot(numFilts+1, 1, numFilts+1); % Create a subplot for the combined   response
plot(t, combined_response, 'k', 'LineWidth', 2); % Plot the combined response   in black with increased line width
xlabel('Time (s)'); % Label the x-axis for the combined response subplot
ylabel('Amplitude - Combined'); % Label the y-axis for the combined response   subplot
title('Combined Response and Individual Filter Responses'); % Set the title for   the combined response plot
% Display contribution of each filter to the combined response
figure; % Create a new figure for the filter contribution plot
bar(CenterFreqs, sum(output_signal, 1), 'b'); % Create a bar graph showing the   contribution of each filter to the combined response
xlabel('Center Frequency (Hz)'); % Label the x-axis for the contribution plot
ylabel('Contribution to Combined Response'); % Label the y-axis for the   contribution plot
title('Filter Contribution to Combined Response'); % Set the title for the   contribution plot

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.

Community Treasure Hunt

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

Start Hunting!

Translated by