Skip to main content
deleted 33 characters in body
Source Link
hari
  • 1
  • 1
  • 2

Here is the code I use to plot a function in frequency domain in Matlab:

    dt = 1/10000; % sampling rate
    et = 0.1; % end of the interval
    t = 0:dt:et; % sampling range
    y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); %     sample the signal
    subplot(2,1,1); % first of two plots
    plot(t,y); grid on % plot with grid
    xlabel('Time (s)'); % time expressed in seconds
    ylabel('Amplitude'); % amplitude as function of time
    Y = fft(y); % compute Fourier transform
    n = size(y,2)/2; % 2nd half are complex conjugates
    amp_spec = abs(Y)/n; % absolute value and normalize
    subplot(2,1,2); % second of two plots
    freq = (0:100)/(2*n*dt); % abscissa viewing window
    stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
    xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
    ylabel('Amplitude'); % amplitude as function of frequency

The problem is, when I zoom in my graph I don't see any peaks exactly on 50Hz, 90Hz and 180Hz as there should be near these points.

What did I do wrong in my code?

Here is the code I use to plot a function in frequency domain in Matlab:

    dt = 1/10000; % sampling rate
    et = 0.1; % end of the interval
    t = 0:dt:et; % sampling range
    y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); %     sample the signal
    subplot(2,1,1); % first of two plots
    plot(t,y); grid on % plot with grid
    xlabel('Time (s)'); % time expressed in seconds
    ylabel('Amplitude'); % amplitude as function of time
    Y = fft(y); % compute Fourier transform
    n = size(y,2)/2; % 2nd half are complex conjugates
    amp_spec = abs(Y)/n; % absolute value and normalize
    subplot(2,1,2); % second of two plots
    freq = (0:100)/(2*n*dt); % abscissa viewing window
    stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
    xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
    ylabel('Amplitude'); % amplitude as function of frequency

The problem is, when I zoom in my graph I don't see any peaks on 50Hz, 90Hz and 180Hz as there should be near these points.

What did I do wrong in my code?

Here is the code I use to plot a function in frequency domain in Matlab:

    dt = 1/10000; % sampling rate
    et = 0.1; % end of the interval
    t = 0:dt:et; % sampling range
    y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); %     sample the signal
    subplot(2,1,1); % first of two plots
    plot(t,y); grid on % plot with grid
    xlabel('Time (s)'); % time expressed in seconds
    ylabel('Amplitude'); % amplitude as function of time
    Y = fft(y); % compute Fourier transform
    n = size(y,2)/2; % 2nd half are complex conjugates
    amp_spec = abs(Y)/n; % absolute value and normalize
    subplot(2,1,2); % second of two plots
    freq = (0:100)/(2*n*dt); % abscissa viewing window
    stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
    xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
    ylabel('Amplitude'); % amplitude as function of frequency

The problem is, when I zoom in my graph I don't see peaks exactly on 50Hz, 90Hz and 180Hz.

What did I do wrong in my code?

added 131 characters in body
Source Link
hari
  • 1
  • 1
  • 2

Here is the code I used foruse to plot frequency spectrum ofa function in frequency domain in Matlab:

dt = 1/10000; % sampling rate et = 0.1; % end of the interval t = 0:dt:et; % sampling range y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.180.t); % sample the signal subplot(2,1,1); % first of two plots plot(t,y); grid on % plot with grid xlabel('Time (s)'); % time expressed in seconds ylabel('Amplitude'); % amplitude as function of time Y = fft(y); % compute Fourier transform n = size(y,2)/2; % 2nd half are complex conjugates amp_spec = abs(Y)/n; % absolute value and normalize subplot(2,1,2); % second of two plots freq = (0:100)/(2ndt); % abscissa viewing window stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second ylabel('Amplitude'); % amplitude as function of frequency

    dt = 1/10000; % sampling rate
    et = 0.1; % end of the interval
    t = 0:dt:et; % sampling range
    y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); %     sample the signal
    subplot(2,1,1); % first of two plots
    plot(t,y); grid on % plot with grid
    xlabel('Time (s)'); % time expressed in seconds
    ylabel('Amplitude'); % amplitude as function of time
    Y = fft(y); % compute Fourier transform
    n = size(y,2)/2; % 2nd half are complex conjugates
    amp_spec = abs(Y)/n; % absolute value and normalize
    subplot(2,1,2); % second of two plots
    freq = (0:100)/(2*n*dt); % abscissa viewing window
    stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
    xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
    ylabel('Amplitude'); % amplitude as function of frequency

ProblemThe problem is, when I zoom in my graph I don't see any peaks on 50Hz, 90Hz and 180Hz as itthere should be, but near these points. 

What did I do wrong in my code?

Here is code I used for plot frequency spectrum of function in Matlab:

dt = 1/10000; % sampling rate et = 0.1; % end of the interval t = 0:dt:et; % sampling range y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.180.t); % sample the signal subplot(2,1,1); % first of two plots plot(t,y); grid on % plot with grid xlabel('Time (s)'); % time expressed in seconds ylabel('Amplitude'); % amplitude as function of time Y = fft(y); % compute Fourier transform n = size(y,2)/2; % 2nd half are complex conjugates amp_spec = abs(Y)/n; % absolute value and normalize subplot(2,1,2); % second of two plots freq = (0:100)/(2ndt); % abscissa viewing window stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second ylabel('Amplitude'); % amplitude as function of frequency

Problem is, when I zoom my graph I don't see peaks on 50Hz, 90Hz and 180Hz as it should be, but near these points. What did I wrong in my code?

Here is the code I use to plot a function in frequency domain in Matlab:

    dt = 1/10000; % sampling rate
    et = 0.1; % end of the interval
    t = 0:dt:et; % sampling range
    y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); %     sample the signal
    subplot(2,1,1); % first of two plots
    plot(t,y); grid on % plot with grid
    xlabel('Time (s)'); % time expressed in seconds
    ylabel('Amplitude'); % amplitude as function of time
    Y = fft(y); % compute Fourier transform
    n = size(y,2)/2; % 2nd half are complex conjugates
    amp_spec = abs(Y)/n; % absolute value and normalize
    subplot(2,1,2); % second of two plots
    freq = (0:100)/(2*n*dt); % abscissa viewing window
    stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
    xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
    ylabel('Amplitude'); % amplitude as function of frequency

The problem is, when I zoom in my graph I don't see any peaks on 50Hz, 90Hz and 180Hz as there should be near these points. 

What did I do wrong in my code?

Source Link
hari
  • 1
  • 1
  • 2

Frequency spectrum of signal - Matlab

Here is code I used for plot frequency spectrum of function in Matlab:

dt = 1/10000; % sampling rate et = 0.1; % end of the interval t = 0:dt:et; % sampling range y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.180.t); % sample the signal subplot(2,1,1); % first of two plots plot(t,y); grid on % plot with grid xlabel('Time (s)'); % time expressed in seconds ylabel('Amplitude'); % amplitude as function of time Y = fft(y); % compute Fourier transform n = size(y,2)/2; % 2nd half are complex conjugates amp_spec = abs(Y)/n; % absolute value and normalize subplot(2,1,2); % second of two plots freq = (0:100)/(2ndt); % abscissa viewing window stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second ylabel('Amplitude'); % amplitude as function of frequency

Problem is, when I zoom my graph I don't see peaks on 50Hz, 90Hz and 180Hz as it should be, but near these points. What did I wrong in my code?