Need Help running functions

1 次查看(过去 30 天)
I need help coding and running some functions for the signal in the attached. I need to find the value of the signal bandwidth using the following bandwidth definitions:
(a) Half-power bandwidth.
(b) Null-to-null bandwidth.
(c) 99% of power bandwidth. (Hint: Use numerical methods.)
(d) Bandwidth beyond which the attenuation is 35 dB.
Below is what I have so far for the code but I get several errors when I try to run. Could you please help.
clc
clear all
f=446000
Gx(f)=10^-4*(sin(pi*(f-10^6)*10^-4)/(pi*(f-10^6)*10^-4))
% calculate half-power beamwidth
function Bw = half_power_bw(f, Gf)
% Find the index of the maximum value in the magnitude spectrum
[~,imax] = max(abs(Gf));
% Find the indices of the half-power points
i_half_lower = find(abs(Gf) < abs(Gf(imax))/sqrt(2), 1, 'first');
i_half_upper = find(abs(Gf) < abs(Gf(imax))/sqrt(2), 1, 'last');
% Calculate the half-power bandwidth
Bw = f(i_half_upper) - f(i_half_lower);
end
% null-to-null beamwidth
function Bw = null_to_null_bw(f, Gf)
% Find the first and last nulls of the magnitude spectrum
nulls = find(abs(Gf) == 0);
i_null_lower = nulls(1);
i_null_upper = nulls(end);
% Calculate the null-to-null bandwidth
Bw = f(i_null_upper) - f(i_null_lower);
end
% 99% of power beamwidth
function Bw = power_bw(f, Gf, power_fraction)
% Calculate the total power
total_power = trapz(f, abs(Gf).^2);
% Find the index where the cumulative power reaches the desired fraction
power_integral = cumtrapz(f, abs(Gf).^2);
i_power = find(power_integral >= total_power*power_fraction, 1, 'first');
% Calculate the bandwidth at the desired power fraction
Bw = f(i_power);
end
% Bandwidth beyond which the attenuation is 35 dB.
function Bw = attenuation_bw(f, Gf, attenuation_dB)
% Convert attenuation from dB to linear scale
attenuation_linear = 10^(-attenuation_dB/20);
% Find the index where the magnitude spectrum falls below the attenuation level
i_attenuation = find(abs(Gf) < abs(max(Gf))*attenuation_linear, 1, 'first');
% Calculate the bandwidth beyond the attenuation level
Bw = f(i_attenuation);
end

采纳的回答

Chetan
Chetan 2024-2-27
编辑:Chetan 2024-2-27
I noticed a few areas in the MATLAB code that we can improve upon to get it running :
  1. In MATLAB, we need to define functions like `Gx(f)` in a specific way. Instead of defining it within the script, let's set it up as an anonymous function to ensure it works correctly.
  2. To properly evaluate `Gx(f)`, we'll need to create a frequency vector `f`. This will allow us to analyse the function over a specified range of frequencies.
  3. When using the `find` function, you have to be careful to handle cases where it might return an empty array, as this could lead to errors when trying to access array elements.
Here is the revised code for the same:
% Clear the workspace and command window
clc;
clear all;
% Define a frequency range to analyze the signal
f = linspace(0, 2*10^6, 1000); % This is an example range from 0 to 2 MHz.
% Set up the signal function as an anonymous function for flexibility
Gx = @(f) 10^-4 .* (sin(pi.*(f-10^6).*10^-4) ./ (pi.*(f-10^6).*10^-4));
% Compute the magnitude spectrum for our signal
Gf = Gx(f);
% Now, let's calculate the bandwidths according to the different definitions
hp_bw = half_power_bw(f, Gf);
nn_bw = null_to_null_bw(f, Gf);
p99_bw = power_bw(f, Gf, 0.99);
att_bw = attenuation_bw(f, Gf, 35);
% We'll display the results in a reader-friendly format
disp(['Half-power Bandwidth: ', num2str(hp_bw)]);
Half-power Bandwidth: 10010.01
disp(['Null-to-Null Bandwidth: ', num2str(nn_bw)]);
Null-to-Null Bandwidth: 2000000
disp(['99% Power Bandwidth: ', num2str(p99_bw)]);
99% Power Bandwidth: 2094094.0941
disp(['Bandwidth beyond 35 dB attenuation: ', num2str(att_bw)]);
Bandwidth beyond 35 dB attenuation: 0
% Here are the function definitions
function Bw = half_power_bw(f, Gf)
% ... your existing code for half_power_bw ...
end
function Bw = null_to_null_bw(f, Gf)
% ... your existing code for null_to_null_bw ...
end
function Bw = power_bw(f, Gf, power_fraction)
% ... your existing code for power_bw ...
end
function Bw = attenuation_bw(f, Gf, attenuation_dB)
% ... your existing code for attenuation_bw ...
end
Refer to the following Mathworks Documentation for usage of anonymous functions & function handles :
Best regards,
Chetan Verma

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by