While calculating power spectral density it is showing an error , vector lengths must be same

I am calculating power spectral density of voltage and time values from an excel sheet of 2 columns and 9689 rows. while plotting the graph it is throwing an error "Error using plot Vectors must be the same length. Error in lpf (line 19) plot(fc,Nc)" Please help me out. I am attaching the code file in the above attachment.

 Risposta accettata

I cannot run your code, however I believe I see the problem.
First, delete this line:
Nc(2:end-1) = 2*Nc(2:end-1);
and change the plot calls to:
plot(fc,Nc(1:length(fc))*2)
and:
plot(fc,10*log10(Nc(1:length(fc))*2));
That should work.

12 Commenti

Still, I cannot run the code. Its because you do not have the excel file. I am attaching the text file of the values because i am unable attach excel file you can have a look. The problem is with the vector length!
Thanks
This works:
t = dataset(:,1);
V = dataset(:,2);
X=[t V];
subplot(2,2,1);
plot(X(:,1), X(:,2))
xlabel ('time');
ylabel('voltage');
title('LPF');
Fs=50;
T=1/Fs; %sampling time
L=length(V); %length of the signal
NFFT=2^nextpow2(L); %next power of 2 from length y
Nc=abs(fft((V-mean(V)),NFFT)).^2./L;
fc=Fs/2*linspace(0,1,NFFT/2+1);%frequency
subplot(2,2,3);
plot(fc,Nc(1:length(fc))*2);
set(gca,'XLim',[0 1.6])
subplot(2,2,4);
plot(fc,10*log10(Nc(1:length(fc))*2));
set(gca,'XLim',[0 1.6])
When I looked earlier, I did not notice this line:
Nc = Nc(1:513);
that ended up causing the error, and is actually not necessary. Since ‘fc(513)’ is about 1.6 Hz, I set that as the upper limit of 'XLim' in the plots. It is much easier to set the axis limits than to plot a subset of the data.
I have a question. I am attaching two data sets for your reference. Both have same lengths of time ie., 3 seconds of data. but they have improperly spaced one file has 770 rows of data of 3 seconds the other has 9670 rows of data of 3 seconds. I need to calculate Power spectral density of these two files. I have tried similarly to the file which has 770 rows of data and I am not getting the graph properly. So how to calculate when the lengths of the data are different and which parameter should I change in the code?
You have to calculate the correct sampling period, and from it the sampling frequency, for each signal separately, and use that. Then, calculate and plot the PSD for each signal separately.
If the sampling interval is not uniform, one option is to use the Signal Processing Toolbox resample function, using a vector you create with a known, regular sampling interval for the interpolation. (Use resample rather than interp1. The resample function uses an anti-aliasing filter, so use it for signal processing applications.)
An easy way to determine the regularity of the sampling intervals is to use:
sampling_sd = std(diff(t));
where ‘t’ is the vector of sampling times. The standard deviation should be close to zero for a regularly-sampled signal. If it is not regularly-sampled, use the resample function to correct for that, then use the resampled signal in your analysis.
In the attached file the sampling period is about 0.004 seconds ., that means my sampling frequency is 1/0.004 = 250 Hz Is it correct? Because the data points are evenly spread with time gaps of .004 seconds
That is correct.
Note that ‘lpfoutput.txt’ is not evenly sampled, so you would have to use resample to get it evenly-spaced.
I would do something like this:
t_resampled = linspace(min(t), max(t), length(t));
V_resampled = resample(V, t);
with the original variables corresponding to those in your posted code. Then use ‘t_resampled’ and ‘V_resampled’ in the rest of your code:
Ts = mean(diff(t_resampled));
Fs = 1/Ts;
I got your point but I dont know where I am doing wrong ! I am not geeting the graph properly. Is there any easier way to get PSD of my data.
dataset=xlsread('lpfoutput.xlsx','sheet1','A1:B9689');
t = dataset(:,1);
V = dataset(:,2);
X=[t V];
subplot(2,2,1);
plot(X(:,1), X(:,2))
xlabel ('time');
ylabel('voltage');
title('LPF');
t_resampled = linspace(min(t), max(t), length(t));
V_resampled = resample(V, t);
subplot(2,2,2);
plot(t_resampled,V_resampled );
Ts = mean(diff(t_resampled));
Fs = 1/Ts;
L=length(V_resampled); %length of the signal
NFFT=2^nextpow2(L); %next power of 2 from length y
Nc=abs(fft((V_resampled-mean(V_resampled)),NFFT)).^2./L;
fc=Fs/2*linspace(0,1,NFFT/2+1);%frequency
subplot(2,2,3);
plot(fc,Nc(1:length(fc))*2);
set(gca,'XLim',[0 1.6])
subplot(2,2,4);
plot(fc,10*log10(Nc(1:length(fc))*2));
set(gca,'XLim',[0 1.6])
You need to set the x-axis limits differently:
set(gca,'XLim',[0 100])
for both plots.
Got it ! thank you very much for your patience I suck at matlab
As always, my pleasure!
No, you don’t. You simply need time with it. None of us entered this workd knowing how to program.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by