- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Plotting multiple plots on the same graph
5 views (last 30 days)
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)')
0 Comments
Answers (1)
Hassaan
on 4 Feb 2024
Edited: Hassaan
on 4 Feb 2024
@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
Feel free to contact me.
6 Comments
See Also
Categories
Find more on 2-D and 3-D Plots 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!