필터 지우기
필터 지우기

Plotting multiple plots on the same graph

조회 수: 1 (최근 30일)
S
S 2024년 2월 4일
댓글: S 2024년 2월 4일
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)')

답변 (1개)

Hassaan
Hassaan 2024년 2월 4일
편집: Hassaan 2024년 2월 4일
@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
S
S 2024년 2월 4일
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 2024년 2월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Digital and Analog Filters에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by