# Is there any easy way to handle difference equation in MATLAB ?

10 次查看（过去 30 天）
Ram 2023-6-28

So currenlty i am working on difference equation and for example i am just taking the differnce equation as :
h(n) = 0.1δ(n) + 0.2δ(n − 2) + 0.5δ(n − 3).
what i have been doing so far is the trivial approrach of iterating over all values of n and making a vector 'h' out of that but that will become useless if the number of n goes to very large .So i am looking currently if i can have a optimistic way to get the frequency/Phase/Magnitude responses.
Thanks for your time and patience.

### 采纳的回答

Harsh Kumar 2023-6-28
Hi Ram,
I understand that you are trying to solve difference equations optimistically in MATLAB.
To do this , you can take the Discrete Time Fourier Transform of ‘h_n’ initially only to convert your system to frequency domain instead of iterating it over the whole values of n.
Please refer to the below code snippet for better understanding.
n=-20:1:20;
%defining the h_n using logic operations
h_n=0.1*(n==0)+0.2*(n==2)+0.5*(n==3);
%frequency
w=-2*pi:(4*pi)/40:2*pi;
subplot(411)
stem(n,(h_n));
title('Original function')
%DTFT
subplot(412)
stem(w,real(dtft(h_n,n,w)));
title('Real part of DTFT')
%Angle plot
subplot(413)
stem(w,angle(dtft(h_n,n,w)));
title('Angle function')
%Magnitude plot
subplot(414)
stem(w,abs(dtft(h_n,n,w)));
title('Magnitude function')
p=dtft(h_n,n,w);
%DTFT function
function[x_w]=dtft(x_n,n,w)
x_w=x_n*(exp((1i)*-1*transpose(n)*w));
end
You can refer to this documentation as well if you want to use the inbuilt DTFT function :- Link

### 更多回答（1 个）

Jacob 2023-6-28
Hi Ram,
Consider looking into the freqz function. An example code that I was able to create is as follows:
% Defining the coefficients of the difference equation
b = [0.1, 0.2, 0.5]; % Numerator coefficients corresponding to the equation you mentioned
a = [1]; % Denominator coefficients (default is 1)
% Compute the frequency response
[h, w] = freqz(b, a);
% Plot the magnitude response
subplot(2, 1, 1);
plot(w, abs(h));
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Response');
% Plot the phase response
subplot(2, 1, 2);
plot(w, angle(h));
xlabel('Frequency');
ylabel('Phase');
title('Phase Response')

### 类别

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

### Community Treasure Hunt

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

Start Hunting!

Translated by