Find transfer function from bode plot in matlab - matlab

I have obtained the bode plot for a system. The system seems to have a very complex magnitude and phase plot. It's not possible to find the transfer function manually. Is there a way of finding the transfer function from the magnitude and phase data, in Matlab?
Here's my code:
%%FFT method for finding Transfer Function
load testdata2.mat;
input = fft(signal(:,1));
% FFT of input data
output = fft(signal(:,2));
% FFT of output data
fft_ratio = output ./ input;
subplot(2,1,1)
%Magnitude
semilogx(20*log10(abs(fft_ratio)))
subplot(2,1,2)
%Phase
semilogx((180/pi)*angle(fft_ratio))
mag = 20*log10(abs(fft_ratio));
phase = (180/pi)*angle(fft_ratio);
Here's my data.

I don't believe so, and that's not Matlab's fault. The problem is mathematically nontrivial because there can be poles and zeros of the transfer function that lie at large imaginary frequency. These might not significantly affect the Bode plot, but how would you rule out their existence?
I think your best bet is to fit the Bode plot to a rational transfer function, and just keep increasing the number of poles and zeros in the transfer function until you get acceptable agreement.

Related

How to Calculate the transfer function and plot it after making fourier transform in frequency domain in terms of magnitude and phase?

I have a trouble of plotting the transfer function in terms of magnitude and phase after making the Fourier transform of two signals.
First, I have used excel xlxs to read the column to plot in time domain and frequency domain, then I calculate the transfer function and this goes fine.
But for magnitude and phase, I have trouble for plotting them. I tired to plot them, but this is totally incorrect. Would someone help me with this?
Here is the code and excel file also.
solarcell1 = xlsread('solarcell.xlsx','A2:C100005');
t=solarcell1(:,1);
N=length(t);
Channel1V = solarcell1(:,2);
Channel2V = solarcell1(:,3);
sig1=Channel1V;
sig2=Channel2V;
fs=1/((solarcell1(3)-solarcell1(2))*1e-3);
FA1=fs/length(sig1);
FA2=fs/length(sig2);
frange1=-fs/2:FA1:fs/2-FA1;
frange2=-fs/2:FA2:fs/2-FA2;
subplot(3,2,1);
plot(t,sig1);
hold on
plot(t,sig2);
title('Input and Output of Solar cell');
xlabel('Time');ylabel('Amplitude');
subplot(3,2,2);
plot(t,sig2);
title('Output');
xlabel('Time');ylabel('Amplitude');
z1=fftshift(fft(sig1))/length(t);
subplot(3,2,3);
plot(frange1,abs(z1));
title('Input');
xlabel('Freq.');ylabel('Amplitude');
z2=fftshift(fft(sig2))/length(t);
subplot(3,2,4);
plot(frange2,abs(z2));
title('Output');
xlabel('Freq.');ylabel('Amplitude');
TFC=z2./z1;
magnitude=20*log(abs(TFC));
phase=atan2(imag(TFC),real(TFC));
subplot(3,2,5);
bode(frange1(1002:N),magnitude(1002:N));
title('Magnitude');
xlabel('Freq.');ylabel('Magnitude');
subplot(3,2,6);
semilogx(frange1(1002:N),phase(1002:N));
title('Phase');
xlabel('Freq.');ylabel('Phase');
I can see three issues in the code you provided.
First, your usage of bode is incorrect. This function takes a dynamic model system as argument, and you gave two vectors frange1(1002:N)and magnitude(1002:N). For now, I suggest you simply use plot instead.
subplot(3,2,5);
plot(frange1(1002:N),magnitude(1002:N));
title('Magnitude');
xlabel('Freq.');ylabel('Magnitude');
Then, your usage of semilogx is also risky when you have negative values for the x-axis. With frange1=-fs/2:FA1:fs/2-FA1; you shouldn't ask for semilogx of it. For now, I suggest plot.
Finally, I suggest you use unwrap to plot the phase.
subplot(3,2,6);
plot(frange1(1002:N),unwrap(phase(1002:N)));
title('Phase');
xlabel('Freq.');ylabel('Phase');
It plots:

how to retrieve original signal from power spectrum

I have calculated power spectrum of signal. the steps are:
FFT of time signal
square of absolute value of FFT/length of signal i.e power spectrum
Now I want to convert it into time domain. What steps should I follow.
The reconstruction of the original signal from the frequency-domain requires both the magnitude and the phase information. So, as you compute the power spectrum and keep only the magnitude, you no longer have all the required information to uniquely reconstruct the original signal.
In other words, we can find examples where different signals have the exact same power spectrum. In that case retrieving which one of those different signals was the original one would thus not be possible.
As a simple illustration, let's say the original signal x is:
x = [0.862209 0.43418 0.216947544 0.14497645];
For sake of argument, let's consider some other signal y, which I've specially crafted for the purpose of this example as:
y = [-0.252234 -0.0835824 -0.826926341 -0.495571572];
As show in the following plots, those two signals might appear completely unrelated:
They do however share the same power spectrum:
f = [0:N-1]/N;
Xf = fft(x,N);
Yf = fft(y,N);
hold off; plot(f, Xf.*conj(Xf)/N, 'b');
hold on; plot(f, Yf.*conj(Yf)/N, 'r:');
xlabel('Normalized frequency');
legend('Px', 'Py')
title('Power spectrum');
As a result, someone who only sees the power spectrum and doesn't know that you started with x, could very well guess that you instead started with y.
That said, the fact that those signals have the same power spectrum could tell you that those signals aren't as unrelated as you might think. In fact those signals also share the same autocorrelation function in the time-domain:
Rx = xcorr(x);
Ry = xcorr(y);
t = [0:length(Rx)-1] - length(x) + 1;
hold off; stem(t, Rx, 'bo');
hold on; stem(t, Ry, 'rx');
legend('Rxx', 'Ryy');
xlabel('lag');
title('Autocorrelation');
This is to be expected since the autocorrelation can be obtained by computing the inverse transform (with ifft) of the power spectrum. This, however, is about as much as you can recover in the time domain. Any signal with this autocorrelation function would be as good a guess as any for the original signal. If you are very motivated you could attempt to solve the set of non-linear equations that are obtained from the definition of the autocorrelation and obtain a list of possibles signals. That would still not be sufficient to tell which one was the original, and as you noticed when comparing my example x and y, there wouldn't be a whole lot to make of it.
The easiest way to see the non-uniqueness of the power (or amplitude) spectrum for describing the time domain signal is that both white noise and the delta function in the time domain have the same power (or amplitude) spectrum - a constant - in the frequency domain.

How to plot bode diagram of a signal using fft in MATLAB?

I want to plot bode diagram of the following system both using bode and fft:
%// System info
num=[0 1]; %// Numerator of z-transform of impulse response of system
den=[1 -0.8]; %// Denominator of z-transform of impulse response of system
I used dbode to plot bode method:
figure(6); dbode(num,den,1) %// 1 is sampling time Ts
As I want to do it from fft method, it gets wrong:
Ts=1;
Fs=1/Ts;
L=length(ym);
NFFT = 2^nextpow2(L); %// Next power of 2 from length of ym
H2=fft(ym,NFFT)./fft(u,NFFT);
f=Fs/2*linspace(0,1,NFFT/2+1);
ww=f*2*pi;
figure(7)
semilogx(20*log10(abs(H2(1:NFFT/2+1))))
figure(10)
semilogx((180/pi)*angle(H2(1:NFFT/2+1)))
Bode diagram using bode:
Any idea
Here is my data (u and ym)
I looked at your data and compared it with the theoretical transfer function in the time-domain and it isn't a bad fit if you ignore some of the data:
t = 1:length(u);
num=[0 1]; %// Numerator of z-transform of impulse response of system
den=[1 -0.8]; %// Denominator of z-transform of impulse response of system
H = tf(num,den,1)
[yy,tt,xx] = step(H,max(t));
plot(t-10,ym-2.2,tt,yy)
You'll notice that I have discarded the time values before 10 and shifted the response values down by about 2.2. This gives the following plot (in Octave):
I suggest you do the same thing when taking the FFT:
L = length(ym(t>=10));
NFFT = 2^nextpow2(L);
H2 = fft(ym(t>=10)-2.2,NFFT)./fft(u(t>=10),NFFT);
f=Fs/2*linspace(0,1,NFFT/2+1);ww=f*2*pi;
[mag,ph,w ] = bode(H);
semilogx(ww,20*log10(abs(H2(1:NFFT/2+1))),w,20*log10(abs(mag)))
The DC level of the transfer function is correct, but the poor FFT technique yields too much noise at (relatively) higher frequencies. tfestimate would be a better choice to estimate the transfer based on the measurement data (again remember to pre-process the data the same way as I have just done here). It is part of the Signal Processing Toolbox.

How to produce a log scale FFT with MatLab

It's my first time performing an FFT within MatLab by experimenting with some example code from the MathWorks website. I was wondering if it was possible to take the code I have and transform the x axis to a log-scale representation rather than linear. I understand most of the code, but it is the x axis line of code that I'm still not 100% sure exactly what it is doing apart from the +1 at the end of the line, which is that fact that MatLab's indexing structure doesn't start on 0.
My code so far is:
[y,fs] = wavread('Wav/800Hz_2sec.wav');
NFFT = 4096;
Y = fft(y,NFFT)/length(y);
f = fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2+1))
frequency usually comes out in linear scale from Discrete Fourier Transform. if you want, you can make a new frequency vector in log scale and interpolate the results you already have
fnew=fs/2.*logspace(log10(fs/length(y)),0,npts);
Ynew= interp1(f,Y(1:NFFT/2+1),fnew);
where npts is the length of your new frequency vector. for just plotting
loglog(f,2*abs(Y(1:NFFT/2+1));
honestly IMO, the interpolation thing doesn't work very well because FFT of real signals produces strong peaks and troughs in spectra, so unless you smooth your spectrum first, the interpolated spectrum won't look as nice

Confusion in figuring out the relation between actual frequency values and FFT plot indexes in MATLAB

I know that there are a lot of similar questions to this, I am still unable to figure out the answer.
Let's say we have time signal in MATLAB:
t=0:1/44100:1
and a cosine signal with frequency 500Hz:
x=cos(2*pi*500*t);
Now, I am trying to plot the magnitude spectrum obtained using the fft command on signal x
FFT=abs(fft(x))
plot(FFT)
According to the theory, we should get two peaks in the plot, one at -500 Hz and the other at 500Hz.
What I don't understand is that I do get two peaks but I can't figure out at what frequencies these peaks are. I know there is a way to figure out the frequency using the FFT index, length of the input signal and the sampling frequency but I still can't calculate the frequency.
I know that there are methods to align the FFT plots so that the peaks lie at the index number of the frequency they represent by using the fftshift function, but what I want is to figure out the frequency using the the plot resulting from simply calling this function:
FFT=fft(x)
In this case, I already know that signal contains a cosine of 500Hz, but what if the signal that we want to get the FFT of is not known before time. How can we get the frequency values of the peaks in that sample using the output from the fft function?
You need to generate the frequency array yourself and plot your FFT result against it.
Like this:
function [Ycomp, fHz] = getFFT(data, Fs)
len = length(data);
NFFT = 2^nextpow2(len);
Ydouble = fft(data, NFFT)/len; % Double-sided FFT
Ycomp = Ydouble(1:NFFT/2+1); % Single-sided FFT, complex
fHz = Fs/2*linspace(0,1,NFFT/2+1); % Frequency array in Hertz.
semilogx(fHz, abs(Ycomp))
end
You will see peaks at 500 Hz and Fs - 500 Hz (i.e. 44100 - 500 = 43600 Hz in your particular case).
This is because the real-to-complex FFT output is complex conjugate symmetric - the top half of the spectrum is a "mirror image" of the bottom half when you are just looking at the magnitude and is therefore redundant.
Note that of plotting power spectra you can usually save yourself a lot of work by using MATLAB's periodogram function rather than dealing directly with all the details of FFT, window functions, plotting, etc.