plot error variables not same length

조회 수: 2 (최근 30일)
Alia Hicks
Alia Hicks 2020년 8월 13일
댓글: hosein Javan 2020년 8월 13일
I'm highly suspicious that the error has to do with line 26, "f = fs*(0:Nsamps/2-1)/Nsamps; " The error I get when I run the program below is:
"Error using plot Vectors must be the same length. Error in CleanAdaptFiltered (line 31) plot(f, y_fft);"
f is a row 1x2000 Nsamps size? I'm not sure of. Something to with the lengthe of t? t is row 1x4001. I've changed too Nsamps = length(ich) and changing line 26 too, "f = fs(1/(dt*Nsamps)*(0:Nsamps));" and "f = 1/(dt*Nsamps)*(0:Nsamps);", same problem.
load AdaptiveFilteredData
d = AdaptiveFilteredData;
fs =100;
dt = .001;
t = 0:dt:4.0001;
%ich = AdaptiveFilteredData.ichannel;
%qch = AdaptiveFilteredData.qchannel;
%plot(ich,qch);
%%PLOTS
%plot(t,ich);
%plot(t,qch);
figure;
plot(t,ich)
xlabel('time(s)');
ylabel('Amplitude');
grid on
Nsamps = length(t); % Window length
y_fft = abs(fft(ich)); %Retain Magnitude
f = fs*(0:Nsamps/2-1)/Nsamps; %Prepare freq data for plot
figure;
plot(f, y_fft); % FFT plot
xlim([0 1]);
xlabel('Frequency(Hz)');
ylabel('FFT Magnitude');
grid on
  댓글 수: 2
Mario Malic
Mario Malic 2020년 8월 13일
Why do you divide by 2 in this line? If you want to plot(x,y), both of them have to have the same number of columns.
f = fs*(0:Nsamps/2-1)/Nsamps;
hosein Javan
hosein Javan 2020년 8월 13일
because the task is to plot a single-sided fourier transform spectrum. see matlab examples of how to use fft.

댓글을 달려면 로그인하십시오.

채택된 답변

hosein Javan
hosein Javan 2020년 8월 13일
try this if could resolve the problem. I have selected those elemnts of "y_fft" corresponding to "f" vector.
plot(f, y_fft(1:length(f))); % FFT plot
  댓글 수: 2
Alia Hicks
Alia Hicks 2020년 8월 13일
Thank you! I did as you suggested and I no longer get an error but the plot of Magnitude vs Freq is blank. Do you think this is because of how y_fft is stored as a single row of entries Nan? I don't know why it is like this it shouldn't be.
hosein Javan
hosein Javan 2020년 8월 13일
don't mention it. y_fft is all nans? then we've got another problem calculating y_fft. yes if it is all nans no wonder the plot is empty.
what about plot(t,ich)?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by