- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
plotting time as legend values with filter as x and amplitude as y
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to make a plot where filter number is my x axis is my filter number and my y axis is amplitude. I am also trying to make a legend where the the t1 time values are what the lines of the plot are. I am not getting an error but I am getting a blank graph and am unsure what to do. Thank you for your time!
close all
clear all
clc
%Version with 3D, x axis filter
fs = 20e3;
numFilts = 32; %
filter_number = numFilts;
order = 4;
CenterFreqs = logspace(log10(50), log10(8000), numFilts);
signal_freq = 100; % Hz
Nperiods = 15; % we need more than 1 period of signal to reach the steady state output (look a the IR samples)
t = linspace(0,Nperiods/signal_freq,200*Nperiods); %
input = sin(2*pi*signal_freq*t) + 0*rand(size(t));
%Impulse
for ii = 1:filter_number
IR = gammatone(order, CenterFreqs(ii), fs);
[tmp,~] = freqz(IR,1,1024*2,fs);
% scale the IR amplitude so that the max modulus is 0 dB
a = 1/max(abs(tmp));
% % or if you prefer - 3dB
% g = 10^(-3 / 20); % 3 dB down from peak
% a = g/max(abs(tmp));
IR_array{ii} = IR*a; % scale IR and store in cell array afterwards
[h{ii},f] = freqz(IR_array{ii},1,1024*2,fs); % now store h{ii} after amplitude correction
plot(IR_array{ii})
end
figure
hold on
t1=[0.02 0.05 0.1]
for ii = 1:numel(filter_number)
output(ii,:) = filter(IR_array{ii},1,input);
plot(filter_number,output(ii,:))
LEGs{ii} = ['Time ' num2str(t1)]; %assign legend name to each
end
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function IR = gammatone(order, centerFrequency, fs)
% Design a gammatone filter
earQ = 9.26449;
minBW = 24.7;
% erb = (centerFrequency / earQ) + minBW;
erb = ((centerFrequency/earQ)^order + (minBW)^order)^(1/order);% we use the generalized
% function (depending on order)
B = 1.019 * erb;
f = centerFrequency;
tau = 1. / (2. .* pi .* B);
% gammatone filter IR
tmax = tau + 20/(2*pi*B);
t = (0:1/fs:tmax);
temp = (t - tau) > 0;
IR = ((t - tau).^(order - 1)) .* exp(-2*pi*B*(t - tau)).* cos(2*pi*f*(t - tau)) .* temp;
end
0 commentaires
Réponses (1)
Hassaan
le 8 Avr 2024
close all;
clear all;
clc;
% Initialization
fs = 20e3; % Sampling frequency
numFilts = 32; % Number of filters
CenterFreqs = logspace(log10(50), log10(8000), numFilts); % Center frequencies
signal_freq = 100; % Signal frequency in Hz
Nperiods = 15; % Number of periods
t = linspace(0, Nperiods/signal_freq, 200*Nperiods); % Time vector
input = sin(2*pi*signal_freq*t); % Input signal
% Impulse Response Calculation
IR_array = cell(1, numFilts);
h = cell(1, numFilts);
for ii = 1:numFilts
IR = gammatone(4, CenterFreqs(ii), fs);
[tmp, ~] = freqz(IR, 1, 1024*2, fs);
a = 1/max(abs(tmp));
IR_array{ii} = IR*a;
[h{ii}, ~] = freqz(IR_array{ii}, 1, 1024*2, fs);
end
% Filtering and Plotting
figure;
hold on;
t1 = [0.02, 0.05, 0.1]; % Example time values for legend
for ii = 1:numFilts
output = filter(IR_array{ii}, 1, input);
plot(abs(h{ii})); % Plotting the magnitude response
LEGs{ii} = ['Filter ' num2str(ii) ', Time ' num2str(t1(mod(ii-1, numel(t1)) + 1))]; % Unique legend for each line
end
legend(LEGs);
hold off;
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Filter Responses with Time Legends');
% Gammatone Filter Function Remains the Same
function IR = gammatone(order, centerFrequency, fs)
% Design a gammatone filter
earQ = 9.26449;
minBW = 24.7;
% erb = (centerFrequency / earQ) + minBW;
erb = ((centerFrequency/earQ)^order + (minBW)^order)^(1/order);% we use the generalized
% function (depending on order)
B = 1.019 * erb;
f = centerFrequency;
tau = 1. / (2. .* pi .* B);
% gammatone filter IR
tmax = tau + 20/(2*pi*B);
t = (0:1/fs:tmax);
temp = (t - tau) > 0;
IR = ((t - tau).^(order - 1)) .* exp(-2*pi*B*(t - tau)).* cos(2*pi*f*(t - tau)) .* temp;
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
3 commentaires
Hassaan
le 8 Avr 2024
@S You can adjust as per your needs.
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!