Asked by Tan
on 2 Jun 2012

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

Answer by Wayne King
on 2 Jun 2012

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])

Wayne King
on 2 Jun 2012

Tan
on 2 Jun 2012

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.

Tan
on 2 Jun 2012

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.

Answer by Wayne King
on 2 Jun 2012

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)))

Tan
on 2 Jun 2012

Wayne King
on 2 Jun 2012

Tan
on 2 Jun 2012

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.

Answer by Wayne King
on 2 Jun 2012

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.

Tan
on 10 Jun 2012

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')

Tan
on 10 Jun 2012

The last three row of codes, is it something wrong there?

Tan
on 11 Jun 2012

Hi, i manage to plot the graph. thanks :)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## shiva prasad kattula (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/40119-convert-time-domain-signal-data-into-frequency-domain-how-to-handle-the-imaginary-terms#comment_347865

Sign in to comment.