# How do i scale to my axis after fft?

8 views (last 30 days)
Cherie Lim on 20 Jan 2020
Commented: Cherie Lim on 21 Jan 2020
My spectrum is of the correct shape, however i am having difficulty in scaling correctly to the frequency domain for the correct frequency bins after fft.
I have comment out the lines of my codes that may require addition of insights and knowledge.
clear all;
close all;
clc;
%% Basic Parameters
w = 0.1; %defined as P_0*tau_0/E_sat or E_in
Fs = 100; %Sampling frequency
tau = -3:1/Fs:3; %0.01 = 1/Fs
alpha = 5;
hold on
%% First iteration
G_0 = 1;
x = (1/2)*sqrt(pi)*w*(1 + erf(tau)); %x = U_in(tau)! %error function!
h = -log(1-(1-(1/G_0))*exp(-x)); %h!
G = G_0./(G_0 -(G_0-1)*exp(-x)); %G!
y = exp(-tau.^2).*G; %y = P_in !
y_out = y.*G; %y = P_out!
phi_out = 0.5*alpha.*h; %phi_out (without phase input)!
% h_dot = -y.*(G-1); %h_dot!
signal = ((y.^0.5).*exp(0.5.*h - (1i .* phi_out)));
% nfft = length(signal); %length of time domain signal
% nfft2 = 2^nextpow2(nfft); %length of signal in power of 2 Need help here!
%need help here!
spectrum = fft(signal);
spectrum = fftshift(spectrum);
spectrum = abs(spectrum).^2;
spectrum_max = max(spectrum);
figure(1);
plot(tau,spectrum/spectrum_max,'g--','LineWidth',0.5);
hold on
%% Subsequent Iterations
for G_0 = [10 100 1000]
x = (1/2)*sqrt(pi)*w*(1 + erf(tau)); %x = U_in(tau)! %error function!
h = -log(1-(1-(1/G_0))*exp(-x)); %h!
G = G_0./(G_0 -(G_0-1)*exp(-x)); %G!
y = exp(-tau.^2).*G; %y = P_in !
y_out = y.*G; %y = P_out!
phi_out = 0.5*alpha.*h; %phi_out (without phase input)! how to obtain phaseinput?
% h_dot = -y.*(G-1); %h_dot!
signal = ((y.^0.5).*exp(0.5.*h - (1i .* phi_out)));
spectrum = fft(signal);
spectrum = fftshift(spectrum);
spectrum = abs(spectrum).^2;
spectrum_max = max(spectrum);
figure(1);
plot(tau,spectrum/spectrum_max,'LineWidth',1);
end
%% Plot-labels
xlabel('(v-v_0)tau','FontSize',14);
ylabel('Normalized power','FontSize',14);
set(gca,'FontSize',14); % size of tick marks on both axes
legend('G_0= Input','G_0= 10 dB','G_0= 20 dB','G_0= 30 dB');
grid on

David Goodmanson on 21 Jan 2020
Hi Cherie,
you have a time array of length, 601 and spacing deltat = .01. The frequency array is going to be 601 points with to-be-determined spacing deltaf. For an N-point fft the basic golden rule for time and frequency array spacing is
deltat*deltaf= 1/N
Equivalently, most people would look at this as
deltat = 1/Fs deltaf = Fs/N
Either way, you can find deltaf. If you use fft shift as you are doing, for an odd number of points the frequency array is
f = (-(N-1)/2:(N-1)/2)*deltaf = (-(N-1)/2:(N-1)/2)*Fs/N
and for an even number of points it would be
f = (-N/2:N/2-1)*deltaf = (-N/2:N/2-1)*Fs/N
It looks like you have nextpow2 in your code, although you are not using it. I would STRONGLY advise against using nextpow2. It can cause a lot of problems to change N (I have seen this many times on this website), and unless you have some crazy large number of points, in my opinion the supposed benefit you get, if any, is stupidly minuscule.

Cherie Lim on 21 Jan 2020
On a side note, my data points seems to be skewed towards the smaller values of the frequency bins, is there any way you suggest which allows me to omit the remaining data there after?
David Goodmanson on 21 Jan 2020
Hi Cherie,
if you mean that most of the action is near f=0 and the data is small as you get away from f=0, that's common. Getting rid of the data won't be good if you ever want to ifft back to the time domain. For plotting purposes you could just use xlim([-f0 f0]) after the plot command, for a suitably chosen f0.
Cherie Lim on 21 Jan 2020
thank you so much! you have been a great help in my progress!