Convert Time Domain Signal Data into Frequency Domain, How to handle the imaginary terms?

Hi Everyone,
I'm a beginner of MATLAB. I'm having some problems of converting time domain signal into frequency domain.
In time domain, is this the way to plot the graph?
value = [42007935 111212895 184546560 238219725 238219725 184546560 111212895 42007935]; time = [0, 1, 2, 3, 4, 5, 6, 7, 8 ]; plot (time, value);
Then, when i want to plot it in frequency domain, i use the following codes:
fft_Conv = fft (value); plot (time, fft_Conv);
I get a warning: Warning: Imaginary parts of complex X and/or Y arguments ignored.
Could anyone guide me how to plot signal in time domain and frequency domain?
Regards, HJ.C

1 Comment

The frequency-domain representation of a signal carries information about the signal's magnitude and phase at each frequency. This is why the output of the FFT computation is complex. A complex number, x, has a real, x_r, and an imaginary part, x_i, such that x = x_r + ix_i. The magnitude of is computed as sqrt{(x_r^2+x_i^2)}, and the phase of x is computed as arctan{(x_i/x_r)}. You can use MATLAB functions abs and angle to respectively get the magnitude and phase of any complex number.

Sign in to comment.

 Accepted Answer

You want to plot the magnitude and phase separately for the complex-valued data. Think of the polar form of a complex number. A few other things: you want to create a frequency vector to use in plotting the frequency domain data (DFT). You may or may not want to center 0 frequency in your Fourier transform, I do this below. Because the mean of your time data is so large, you are going to get a large 0 frequency magnitude in your Fourier transform.
value = [42007935 111212895 184546560 238219725 238219725 184546560 111212895 42007935];
time = 0:7;
stem(time,value,'markerfacecolor',[0 0 1])
title('Time Data'); xlabel('Time'); ylabel('Amplitude');
dftvalue = fft(value);
freq = -pi:(2*pi)/length(value):pi-(2*pi)/length(value);
figure;
stem(freq,abs(fftshift(dftvalue)),'markerfacecolor',[0 0 1])
title('Magnitude of DFT'); xlabel('Frequency'); ylabel('Magnitude');
figure;
stem(freq,angle(fftshift(dftvalue)),'markerfacecolor',[0 0 1])
title('Phase of DFT');
xlabel('Frequency'); ylabel('Magnitude');
% or stem(freq,unwrap(angle(dftvalue)),'markerfacecolor',[0 0 1])

7 Comments

Hi Wayne King,
So glad to get your reply :)
Could you roughly explain how the code works?
Really appreciate your help !
Mr Wayne King,
stem(time,value,'markerfacecolor',[0 0 1]);
What does this line means?
Since you have so few data points, I think a stem plot looks better than a line plot here. The 'markerfacecolor',[0 0 1] part is just filling in the circle on the stem plot with the color blue
Hi Mr Wayne King,
The values in the value array are actually the numbers of a filtered signal in time domain.
Is it possible to use those value to plot the frequency response?
Regards,
HJ.C
No, not unless your system is a linear shift-invariant system and the input signal was an impulse. Under those circumstances, the filtered signal would be the impulse response and you could get the frequency response.
The values in the value array are actually the result of an impulse with 1V passing through a low pass filtered with frequency coefficients shown below.
Numerator: Denominator:
[3.91719822748777E-02, [1.000]
0.103629842929331,
0.171922134825388,
0.221881476438683,
0.221881476438683,
0.171922134825388,
0.103629842929331,
3.91719822748777E-02]
I intend to see if the frequency response of the impulse after pass through the filter really response like what i expect.
Numerator:
[3.91719822748777E-02,
0.103629842929331,
0.171922134825388,
0.221881476438683,
0.221881476438683,
0.171922134825388,
0.103629842929331,
3.91719822748777E-02]
Denominator:
[1.000]

Sign in to comment.

More Answers (4)

Then you want to use freqz()
b = [3.91719822748777E-02,
0.103629842929331,
0.171922134825388,
0.221881476438683,
0.221881476438683,
0.171922134825388,
0.103629842929331,
3.91719822748777E-02];
[H,W] = freqz(b,1);
plot(W,20*log10(abs(H)))

4 Comments

I have one more doubt, can we plot the frequency response of that impulse if the filter coefficient is not given? as in, assume that the filter is a black box, we don't know the coefficient, we just feed an impulse to the filter, and values in value array are what we get at the output of filter. Can we use those values to plot back the frequency of the filtered impulse?
Because i'm actually want to prove if the filtered impulse is following/producing output based on the filter behavior (coefficient). Can we do that?
If you feed it the impulse AND the system is LTI, then yes, the Fourier transform of the impulse response is the frequency response. Just keep in mind that by giving me that b vector above you are asserting that is the impulse response and that system has a finite impulse response
Yes. My system a linear shift-invariant system and the input signal is an impulse.
So if i want to plot the response out when we are just given:
value = [42007935 111212895 184546560 238219725 238219725 184546560 111212895 42007935];
How should i do?
I'm sorry if i ask a repeating question, because i have vague idea on this

Sign in to comment.

Then you are telling me that
b = [42007935 111212895 184546560 238219725 238219725 184546560 111212895 42007935];
% so
[h,f] = freqz(b,1);
plot(f,20*log10(abs(h)));
Of course if you know the sampling frequency, then you can use that in freqz() to get the response in Hz.

9 Comments

It looks like the waveform i want. If i want to change the range of the x-axis to 0-5? what should i do?
Can i say that the signal has cut off frequency at around 0.5 or 1?
Hi Mr Wayne King,
Why the cut off frequency of the impulse frequency response are different when i plot them using the manual way (a) and fdatool(just paste the filter coefficient and click import filter button) with the same set of filter coefficient.
(a) b = [3.91719822748777E-02,
0.103629842929331,
0.171922134825388,
0.221881476438683,
0.221881476438683,
0.171922134825388,
0.103629842929331,
3.91719822748777E-02];
[H,W] = freqz(b,1);
plot(W,20*log10(abs(H)))
title('Frequency Response of Filter (Generated by SystemVue)');
xlabel('Frequency(Hz)'); ylabel('Magnitude(dB)');
Wayne King's responses have been very helpful in furthering my understanding of handling the FFT and mag/phase of the response and how to handle them in MATLAB. Good work.
Hi Wayne King, may i know to plot two sets of data in a single graph?
Graph 1 : Numerator:
b_s[3.91719822748777E-02,
0.103629842929331,
0.171922134825388,
0.221881476438683,
0.221881476438683,
0.171922134825388,
0.103629842929331,
3.91719822748777E-02]
Denominator:
[1.000]
+
b_r = [42007935 111212895 184546560 238219725 238219725 184546560 111212895 42007935];
plot(x,y,'b',x2,y2,'r') % x,y are blue, x2,y2 are red
Hi Ryan,
I encounter some problem while plotting the graph
%Plot impulse before filtering in time and frequency domain
b = [3.91719822748777E-02,
0.103629842929331,
0.171922134825388,
0.221881476438683,
0.221881476438683,
0.171922134825388,
0.103629842929331,
3.91719822748777E-02];
time = 0:7;
stem(time,b,'markerfacecolor',[0 0 1])
title('Time Data'); xlabel('Time(s)'); ylabel('Amplitude');
dftb = fft(b);
freqb = -pi:(2*pi)/length(b):pi-(2*pi)/length(b);
figure;
stem(freqb,abs(fftshift(dftb)),'markerfacecolor',[0 0 1])
title('Magnitude of DFT'); xlabel('Frequency(Hz)'); ylabel('Magnitude');
figure;
stem(freqb,angle(fftshift(dftb)),'markerfacecolor',[0 0 1])
title('Phase of DFT');
xlabel('Frequency(Hz)'); ylabel('Magnitude');
%Plot impulse after filtering in time and frequency domain
value = [42007935 111212895 184546560 238219725 238219725 184546560 111212895 42007935];
time = 0:7;
stem(time,value,'markerfacecolor',[1 0 0])
title('Time Data'); xlabel('Time(s)'); ylabel('Amplitude');
dftvalue = fft(value);
freq = -pi:(2*pi)/length(value):pi-(2*pi)/length(value);
figure;
stem(freq,abs(fftshift(dftvalue)),'markerfacecolor',[1 0 0])
title('Magnitude of DFT'); xlabel('Frequency(Hz)'); ylabel('Magnitude');
figure;
stem(freq,angle(fftshift(dftvalue)),'markerfacecolor',[1 0 0])
title('Phase of DFT');
xlabel('Frequency(Hz)'); ylabel('Magnitude');
% or stem(freq,unwrap(angle(dftvalue)),'markerfacecolor',[0 0 1])
%Plot two sets of data in one graph (time domain)
stem(time,value,'markerfacecolor',[1 0 0],'r',time,b,'markerfacecolor',[0 0 1],'b')
stem(freq,abs(fftshift(dftvalue)),'markerfacecolor',[1 0 0],'r',freqb,abs(fftshift(dftb)),'markerfacecolor',[0 0 1],'b')
stem(freq,angle(fftshift(dftvalue)),'markerfacecolor',[1 0 0],'r',freqb,angle(fftshift(dftb)),'markerfacecolor',[0 0 1],'b')
The last three row of codes, is it something wrong there?
Hi, i manage to plot the graph. thanks :)

Sign in to comment.

Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
figure(1)
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)'
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
figure(2)
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

Categories

Asked:

Tan
on 2 Jun 2012

Answered:

on 7 Feb 2025

Community Treasure Hunt

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

Start Hunting!