Dear community,
I am not good in signal processing. I need to calculate a single sided power spectral density of a signal, which is called 'g' in my code, but i am not able to get it completely correct (Sampling frequency issues i gues).
%% Investigate window function
clear
close
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Params %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Tleng = 50; % simulated time length [ms]
dt = 0.01; % time step [ms]
% time vector
t = (0:round(Tleng/dt)-1)*dt; % time vector
spike = zeros(size(t));
spike(1) = 1; % Make amplitude
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Generate alpha function %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tau = 3.2; Amp = 1; window_shape = {'alpha'};
g = Synapse (spike, dt, tau, Amp, window_shape);
subplot(2,1,1)
plot ( t , g )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% DFT (Internet example which is probalbly not correct) %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fs=100000; %sampling frequency
NFFT = 1024;
X = fft(g,NFFT);
X = X(1:NFFT/2+1);%Throw the samples after NFFT/2 for single sided plot
Pxx=X.*conj(X)/(NFFT*NFFT);
f = fs*(0:NFFT/2)/NFFT; %Frequency Vector
subplot(2,1,2)
plot(f/1000,10*log10(Pxx),'r');
title('Single Sided - Power Spectral Density');
xlabel('Frequency (kHz)')
ylabel('Power Spectral Density- P_{xx} dB/Hz');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Function for Synapse %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = Synapse (spikes, dt, tau, Asyn, window_shape)
% This synapse is build to stimulate the IF-Model.
% *** INPUT PARAMETERS ***
% spikes: : spike vector (to show the number of inputs at each step)
% dt : size of time step [ms]
% c : input amplitude [coulomb]
% tau : time constant [ms]
% Asyn : synaptic input amplitude
% window_shape : determination of window
%
% *** OUTPUT PARAMETERs ***
% g : simulated synaptic input waveform [nS]
% Select window shape
window_shape = cell2mat(window_shape);
switch window_shape
case 'alpha'
% creating output vector
g = zeros(1,length(spikes));
% initial values
x = 0;
y = 0;
% factors used for calculation
format long % Genrate more digits
eulers_number = exp(1); % Generate eulers number
format short % Reduce number of digits for better speed
tau = tau / eulers_number; % Standardise with factor. This factor is based on eulers number
aa = Asyn * exp(1.0) / tau; % amplitude factor for normalizing the input
ee = exp(-dt/tau); % decrease factor used at each time step
% step-by-step calculation
for t = 1:length(spikes)
y = ee*y + aa*spikes(t);
x = ee*x + dt*y;
g(t) = x; % store data
end
end
end