Plotting multiple plots on the same graph
Show older comments
So I am trying to plot this noisy data, and the output from each individula filter on the same graph. The filters are in cascade so the output to one is the input to the other. The first figure works, but the second one I can't seem to fix. I added hold on/off which didnt help and don't know what to try next. Thank you for your time!
fs = 20e3;
numFilts = 32;
filter_number = 16;
t = linspace(0,2*pi,200);
input = sin(t) + 0.25*rand(size(t));
%
figure
hold on
for ii = 1:filter_number
output = gammatone(input, CenterFreqs(ii), fs);
plot(output)
LEGs{ii} = ['Filter # ' num2str(ii)]; %assign legend name to each
legend(LEGs{:})
legend('Show')
end
figure %This one!!
plot(t, input, 'k-', 'LineWidth', 2, 'DisplayName', 'Data')
plot(t,output(ii,:), 'LineWidth', 1.25)
title('Signal after Filters up to Filter', num2str(filter_number));
xlabel('t')
ylabel('x(t) & x_{filtered} (t)')
Answers (1)
@S Try this and let me know:
fs = 20e3;
numFilts = 32;
filter_number = 16;
t = linspace(0, 2*pi, 200);
input = sin(t) + 0.25*rand(size(t));
% Define the center frequencies for the filters (example: linearly spaced)
lowFreq = 200; % lowest center frequency
highFreq = fs/2; % highest center frequency (Nyquist frequency)
CenterFreqs = linspace(lowFreq, highFreq, numFilts);
% Initialize the cumulative output variable
cumulative_output = input;
figure
hold on
% Apply the filters in cascade and plot the outputs
for ii = 1:filter_number
output = gammatone(cumulative_output, CenterFreqs(ii), fs);
cumulative_output = output; % Update the cumulative output
plot(t, output, 'LineWidth', 1.25)
LEGs{ii} = ['Filter # ' num2str(ii)]; % Assign legend name to each
end
% Add the original data to the plot
plot(t, input, 'k-', 'LineWidth', 2, 'DisplayName', 'Original Data')
legend(LEGs{:}, 'Original Data') % Add the original data to the legend
hold off
title(['Signal after Filters up to Filter ' num2str(filter_number)]);
xlabel('t')
ylabel('x(t) & x_{filtered} (t)')
The gammatone function you're using should be correctly defined or available in your MATLAB path for the script to work.
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
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.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
6 Comments
S
on 4 Feb 2024
@S Try this:
fs = 20e3;
numFilts = 32;
filter_number = 16;
t = linspace(0, 2*pi, 200);
input = sin(t) + 0.25*rand(size(t));
% Define the center frequencies for the filters (example: linearly spaced)
lowFreq = 200; % lowest center frequency
highFreq = fs/2; % highest center frequency (Nyquist frequency)
CenterFreqs = linspace(lowFreq, highFreq, numFilts);
% Initialize a variable to store the output of each filter
outputs = zeros(filter_number, length(t));
% Apply the filters in cascade
for ii = 1:filter_number
if ii == 1
output = gammatone(input, CenterFreqs(ii), fs);
else
output = gammatone(outputs(ii-1, :), CenterFreqs(ii), fs);
end
outputs(ii, :) = output; % Store the output of each filter
end
% Now plot the original data and the output of each filter
figure;
hold on;
% Plot the original data
plot(t, input, 'k-', 'LineWidth', 2, 'DisplayName', 'Original Data');
% Plot the output of each filter
for ii = 1:filter_number
plot(t, outputs(ii, :), 'LineWidth', 1.25, 'DisplayName', ['Filter # ' num2str(ii)]);
end
% Add legend and labels
legend('show');
title(['Signal after Filter ' num2str(filter_number)]);
xlabel('t');
ylabel('x(t) & x_{filtered} (t)');
hold off;
- An outputs matrix that stores the output of each filter.
- A loop that applies each filter in series, where each filter's input is the output of the previous filter.
- After all filters have been applied, we plot the original data and the outputs from each filter on the same graph.
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
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.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
S
on 4 Feb 2024
@S A potential problem with the gammatone function's application or its response to the input signals.
- Test the gammatone Function Individually: Before applying it in a loop, test the gammatone function with a single frequency to verify it produces a meaningful output. This will help determine if the issue lies with the function itself or its usage in the loop.
- Visualize Filter Response: Visualize the frequency response of the gammatone filter for a few center frequencies to ensure they are behaving as expected (i.e., filtering around the intended center frequency).
- Check for Overlapping Filters: If the filters' frequency responses do not overlap with the input signal's frequency content, subsequent filtering stages might further attenuate the signal, leading to flat outputs. This is particularly relevant if your input signal's frequency content does not match the filters' designed center frequencies.
- Adjust Filter Parameters: If the filters are designed correctly but their parameters (center frequencies, bandwidths, etc.) are not suitable for the input signal, consider adjusting these parameters. Ensuring that at least some of the filters are centered around frequencies present in your input signal can help.
Without the specific implementation of the gammatone function, these suggestions are generalized troubleshooting steps.
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
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.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
S
on 4 Feb 2024
S
on 4 Feb 2024
Categories
Find more on Measurements and Spatial Audio 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!