Apply Filter to Signal

I want to use the built in gammatone filterbank and run a signal through it. I then want to take that output and get the impulse response which should decay over time. But I am running into issues. I think the output_signal line is the issue but I don't know what to use instead. Thank you for your time!
fs = 16e3;
numFilts=32;
range=[50 8000];
gammaFiltBank = gammatoneFilterBank(range,numFilts);
fvtool(gammaFiltBank)
Error using gammatoneFilterBank/fvtool
This functionality is not available on remote platforms.
input_signal = sin(2*pi*100*t) + sin(2*pi*300*t);
output_signal = gammatoneFilterBank(gammaFiltBank, input_signal);
impulse_response = impulse(output_signal);

 Accepted Answer

I think this is the correct way to generate the output signal
fs = 16e3;
t = 0:(1/fs):1; % not defined in question
numFilts=32;
range=[50 8000];
gammaFiltBank = gammatoneFilterBank(range,numFilts);
%fvtool(gammaFiltBank)
input_signal = sin(2*pi*100*t) + sin(2*pi*300*t);
%output_signal = gammatoneFilterBank(gammaFiltBank, input_signal);
output_signal = gammaFiltBank(input_signal);
The next step is "take that output and get the impulse response which should decay over time."
I don't understand this statement. Do you just want the impulse response of each filter in the bank? Are you trying to figure out the impulse response of each filter based on the output_signal and input_signal?
%impulse_response = impulse(output_signal);

10 Comments

S
S on 17 Jan 2024
I am trying to plot the output_signal
then get the impulse response of the filter which should decay over time (impulses should get smaller then go to zero
fs = 16e3;
t = 0:(1/fs):1; % not defined in question
numFilts=32;
range=[50 8000];
gammaFiltBank = gammatoneFilterBank(range,numFilts);
%fvtool(gammaFiltBank)
input_signal = sin(2*pi*100*t) + sin(2*pi*300*t);
%output_signal = gammatoneFilterBank(gammaFiltBank, input_signal);
output_signal = gammaFiltBank(input_signal);
Here's one way to plot the output of the 10th filter. Adapt as needed to plot the output of whichever filters you want.
output_signal = squeeze(output_signal);
figure
plot(t,output_signal(10,:))
The code above can be easily adpated to make the input_signal an unit pulse input, and then plot the impulse response of whichever filters you want.
S
S on 17 Jan 2024
Edited: Walter Roberson on 17 Jan 2024
so like this?
% Impulse signal
impulse_signal = zeros(size(t));
impulse_signal(1) = 1;
output_signal2=gammaFiltBank(impulse_signal)
figure
plot(t,output_signal2(10,:)) %10th filter output
but this gives:
Index in position 1 exceeds array bounds.
Error in t116 (line 22)
plot(t,output_signal2(10,:)) %10th filter output
So I am doing:
% Impulse signal
impulse_signal = zeros(size(t));
impulse_signal(1) = 1;
%Display a Filter's Output of Impulse
output_signal2=gammaFiltBank(impulse_signal);
output_signal2 = squeeze(output_signal2);
figure
plot(t,output_signal2(10,:)) %10th filter output
title('Impulse Output of Filter', num2str(filter_number))
But this output looks the same as the image you added above it is not decaying as it should.
Apparently the input to the filter bank should be a column vector for this problem
fs = 16e3;
t = 0:(1/fs):0.03; % not defined in question
t = t(:); % ensure column vector
numFilts = 32;
range = [50 8000];
gammaFiltBank = gammatoneFilterBank(range,numFilts,fs); % set fs explicity, even though 16e3 is the default
%fvtool(gammaFiltBank)
input_signal = sin(2*pi*100*t) + sin(2*pi*300*t);
%output_signal = gammatoneFilterBank(gammaFiltBank, input_signal);
output_signal = gammaFiltBank(input_signal);
figure
stem(t,output_signal(:,10));
% The impulse response for all filters can be viewed by
% fvtool(gammaFiltBank), then click Analysis -> Impulse Response, or click
% the Impulse Response button in the toolbar
% Or generate the impulse response, make sure to reset the filter
impulse_input = 0*t; impulse_input(1) = 1;
reset(gammaFiltBank); % IMPORTANT!
yimp = gammaFiltBank(impulse_input);
figure
stem(t,yimp(:,10)) % Impulse response of 10th filter
% Or can use impz
[b,a] = gammaFiltBank.coeffs;
figure
impz([b(:,:,10) ones(4,1) a(:,:,10)],numel(t),fs)
S
S on 17 Jan 2024
Thank you for your help! I have been trying to fix this for so long! To get the frequency response couls I repeat the last segment with freqz instead of impz? Or would it be better to do fft of the impz output?
S
S on 17 Jan 2024
I'm doing Freq_Resp=fft(yimp); stem(t,Freq_Resp(:,filter_number));
but the output looks incorrect
S
S on 18 Jan 2024
That was very helpful! Thanks!
S
S on 18 Jan 2024
To get the power of each filter to then turn this into a spectrogram, would I square the output signal in each filter then add them together?

Sign in to comment.

More Answers (1)

  1. Generation of input_signal: The time vector t is not defined in your code. You need to define it to generate input_signal.
  2. Using gammatoneFilterBank: The way you're using gammatoneFilterBank to process input_signal is incorrect. You should use the process function on the filter bank object to process the input signal.
  3. Getting the Impulse Response: To obtain the impulse response, you should create an impulse signal and then process it through the gammatone filter bank. The impulse function is typically used for obtaining the impulse response of a system, but in MATLAB's Audio Toolbox, you'd need to manually create an impulse and pass it through the filter bank.
fs = 16e3; % Sampling frequency
numFilts = 3; % Number of filters
range = [50 8000]; % Frequency range
t = 0:1/fs:1; % Time vector, for 1 second
% Create a Gammatone filter bank
gammaFiltBank = gammatoneFilterBank('SampleRate', fs, 'NumFilters', numFilts, 'FrequencyRange', range);
fvtool(gammaFiltBank);
% Create input signal
input_signal = sin(2*pi*100*t) + sin(2*pi*300*t);
% Process the input signal through the Gammatone filter bank
output_signal = process(gammaFiltBank, input_signal');
% To get the impulse response, create an impulse signal
impulse_signal = [1; zeros(length(t)-1, 1)]; % A single '1' followed by zeros
% Process the impulse signal through the filter bank
impulse_response = process(gammaFiltBank, impulse_signal);
% You can plot the impulse response to see how it decays over time
plot(impulse_response);
xlabel('Samples');
ylabel('Amplitude');
title('Impulse Response of Gammatone Filter Bank');
In this code, impulse_signal is created as a single '1' followed by zeros, which represents an impulse. This impulse is then processed through the gammatone filter bank to get the impulse response. The response is plotted to visualize how it decays over time. Remember that process function is used with the filter bank object to process signals.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.

5 Comments

S
S on 16 Jan 2024
Thank you for your response! This gives me the following error:
Undefined function 'process' for input arguments of type 'gammatoneFilterBank'.
Error in parallel_filterbank (line 146)
output_signal = process(gammaFiltBank, input_signal');
Hassaan
Hassaan on 16 Jan 2024
Edited: Hassaan on 16 Jan 2024
I used and tested it on MATLAB r2023b on desktop[offline].
  1. MATLAB Audio Toolbox: This toolbox is essential for advanced audio processing. It includes functions and tools for audio signal processing, speech analysis, and acoustic measurement. It likely contains the gammatoneFilterBank function, or similar auditory filter bank functions, along with utilities for analyzing and processing audio signals.
  2. Signal Processing Toolbox: This toolbox is specifically geared towards audio and acoustic signals, the Signal Processing Toolbox provides a more general set of tools for signal processing, including filtering, windowing, spectral analysis, and statistical analysis, which can be helpful in conjunction with audio processing.
  3. MATLAB Version Compatibility: Ensure that your MATLAB version is compatible with these toolboxes. Newer functions in these toolboxes might not be available in older MATLAB versions.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
S
S on 16 Jan 2024
I am also using R2023b. I just opened a new file and ran it and am still getting:
Incorrect number or types of inputs or outputs for function process.
Error in t116 (line 14)
output_signal = process(gammaFiltBank, input_signal');
S
S on 16 Jan 2024
Yes, what you have shown is the fvtool not the plot of the impulse response
S
S on 16 Jan 2024
Are you able to get the impluse response plot on your end?

Sign in to comment.

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Tags

Asked:

S
S
on 16 Jan 2024

Commented:

S
S
on 18 Jan 2024

Community Treasure Hunt

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

Start Hunting!