Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need to calculate power spectral density of a signal in MATLAB

Status
Not open for further replies.

gauravnanda18

Newbie level 1
Joined
Apr 19, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,294
what I have to do is to calculate the noise in a signal and see how it depends on the frequency spectrum. I am trying to calculate PSD of a signal but everytime, I get an error saying "vectors must be the same lengths". I am not able to find a solution.

Here is command which I'm using. please let me know if there is some problem with the command.

I imported the excel file as vectors.

x=time; %time vector in excel file

y=current; %current vector in excel file

plot(x,y); % plot current-time graph

xlabel('time(s)');

ylabel('current(A)');

Fs=50; %sampling frequency

T=1/Fs; %sampling time

L=length(y); %length of the signal

L1= x/T;

t=(0:L-1)*T; time vector

%%%calculate the mean current

Iavg=mean(L);

%calculate the FFT

NFFT=2^nextpow2(L); %next power of 2 from length y

% calculate and plot PSD of conditioning mode

Nc=abs(fft((y-mean(y)),NFFT)).^2./NFFT; % Nc is power spectral density

Nc = Nc(1:513);

Nc(2:end-1) = 2*Nc(2:end-1);

fc=Fs/2*linspace(0,1,NFFT/2+1);%frequency

plot(fc,Nc)


ERROR MESSAGE WHICH I GET IS "INDEX EXCEEDS MATRIX DIMENSIONS"
 

what I have to do is to calculate the noise in a signal and see how it depends on the frequency spectrum. I am trying to calculate PSD of a signal but everytime, I get an error saying "vectors must be the same lengths". I am not able to find a solution.

Here is command which I'm using. please let me know if there is some problem with the command.

I imported the excel file as vectors.

x=time; %time vector in excel file

y=current; %current vector in excel file

plot(x,y); % plot current-time graph

xlabel('time(s)');

ylabel('current(A)');

Fs=50; %sampling frequency

T=1/Fs; %sampling time

L=length(y); %length of the signal

L1= x/T;

t=(0:L-1)*T; time vector

%%%calculate the mean current

Iavg=mean(L);

%calculate the FFT

NFFT=2^nextpow2(L); %next power of 2 from length y

% calculate and plot PSD of conditioning mode

Nc=abs(fft((y-mean(y)),NFFT)).^2./NFFT; % Nc is power spectral density

Nc = Nc(1:513);

Nc(2:end-1) = 2*Nc(2:end-1);

fc=Fs/2*linspace(0,1,NFFT/2+1);%frequency

plot(fc,Nc)


ERROR MESSAGE WHICH I GET IS "INDEX EXCEEDS MATRIX DIMENSIONS"

Try using the MATLAB 'pwelch' function. It uses modified welch periodogram to calculate power spectral density
 

what I have to do is to calculate the noise in a signal and see how it depends on the frequency spectrum. I am trying to calculate PSD of a signal but everytime, I get an error saying "vectors must be the same lengths". I am not able to find a solution.

Here is command which I'm using. please let me know if there is some problem with the command.

I imported the excel file as vectors.

x=time; %time vector in excel file

y=current; %current vector in excel file

plot(x,y); % plot current-time graph

xlabel('time(s)');

ylabel('current(A)');

Fs=50; %sampling frequency

T=1/Fs; %sampling time

L=length(y); %length of the signal

L1= x/T;

t=(0:L-1)*T; time vector

%%%calculate the mean current

Iavg=mean(L);

%calculate the FFT

NFFT=2^nextpow2(L); %next power of 2 from length y

% calculate and plot PSD of conditioning mode

Nc=abs(fft((y-mean(y)),NFFT)).^2./NFFT; % Nc is power spectral density

Nc = Nc(1:513);

Nc(2:end-1) = 2*Nc(2:end-1);

fc=Fs/2*linspace(0,1,NFFT/2+1);%frequency

plot(fc,Nc)


ERROR MESSAGE WHICH I GET IS "INDEX EXCEEDS MATRIX DIMENSIONS"



The error is in your time vector.. It should like this >>

t=0:1/Fs:(length(y)-1)/Fs;

Good luck..
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top