Plotting multiple plots on the same graph

5 Ansichten (letzte 30 Tage)
S
S am 4 Feb. 2024
Kommentiert: S am 4 Feb. 2024
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)')

Antworten (1)

Hassaan
Hassaan am 4 Feb. 2024
Bearbeitet: Hassaan am 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
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  6 Kommentare
S
S am 4 Feb. 2024
The plot of the filters themselves looks correct! So could the issue be the way I am trying to plot it or the way I wrote the loop?
S
S am 4 Feb. 2024
I even tried to do just:
figure
plot(t,output(ii,:), 'LineWidth', 1.25)
But that gives:
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in t131 (line 39)
plot(t,output(ii,:), 'LineWidth', 1.25)
which I dont understand as my filter_number is set to 16 not 1

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by