Question on create array of number / transfer function

2 views (last 30 days)
So I want to create a transfer function H(w)=1 for w goes from -2000 to 2000...How do I do that? I need an array of 20000. For w<-2000 or w>2000, I want 0. I only want 1 when w is between -2000 and 2000.
Right now I have H=zeros(1, 20001);
Thanks

Answers (1)

BhaTTa
BhaTTa on 24 Jul 2024
To create a transfer function ( H(w) ) that is 1 for ( w ) in the range ([-2000, 2000]) and 0 otherwise, you can use the following MATLAB code. This code will create an array H of size 20001, where the values are set according to the specified conditions.
Here's the complete MATLAB code:
clc;
clear all;
close all;
% Define the frequency range
w = linspace(-5000, 5000, 20001); % Create an array of 20001 points from -5000 to 5000
% Initialize the transfer function H(w) with zeros
H = zeros(1, 20001);
% Set H(w) to 1 for -2000 <= w <= 2000
H(w >= -2000 & w <= 2000) = 1;
% Display the result
disp('Transfer function H(w):');
disp(H);
% Plot the transfer function
figure;
plot(w, H);
xlabel('Frequency (w)');
ylabel('H(w)');
title('Transfer Function H(w)');
grid on;
Explanation
  1. Define the Frequency Range: Use linspace to create an array w of 20001 points ranging from -5000 to 5000. This ensures that we have enough points to cover the range and set the values accordingly.
  2. Initialize the Transfer Function: Create an array H of zeros with the same length as w.
  3. Set the Values of H(w): Use logical indexing to set the values of H to 1 for the range ([-2000, 2000]). The condition w >= -2000 & w <= 2000 identifies the indices where w is within the desired range.
  4. Display the Result: Use disp to display the resulting transfer function array.
  5. Plot the Transfer Function: Plot the transfer function using plot to visualize the values of H(w) over the frequency range.
Notes
  • The linspace function generates linearly spaced vectors. Here, it is used to create a range of frequencies from -5000 to 5000 with a total of 20001 points.
  • The logical indexing w >= -2000 & w <= 2000 is used to set the values of H to 1 for the specified range of w.
  • The plot helps to visualize the transfer function, showing that it is 1 within the specified range and 0 outside of it.

Community Treasure Hunt

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

Start Hunting!