spectrum(x, log="no",span=5, plot=FALSE) similar command in Matlab.

조회 수: 2 (최근 30일)
Zakia Hossain
Zakia Hossain 2024년 5월 7일
답변: William Rose 2024년 5월 8일
My Matlab code is like following:
del = 1/120; % lenght of sampling interval
subplot(3,1,1)
I= pop(:,2);
I1= I(4801:end)/10000;
plot(t(4801:end),I1,LineWidth=1.5);
xlabel 'Time';
ylabel 'Infectious population, I';
subplot(3,1,2)
S = abs(fft(I1));
L = length(S);
S_oneside= S(1:L/2);
f = (1/del)*(0:L/2-1)/L; %frequency question
S_meg=abs(S_oneside)/(L/2);
P=findpeaks(S_meg);
plot(f,S_meg,LineWidth=1.5);
xlabel 'Frequency';
ylabel 'Spectral density';
subplot(3,1,3)
plot(f,S_meg,LineWidth=1.5)
xlim([0.1 2])
xlabel 'Frequency';
ylabel 'Spectral density';
This gives me the correct graph. But in R for smoothing they use span = 5, how can I smooth the peaks in Matlab?

답변 (1개)

William Rose
William Rose 2024년 5월 8일
The curve fiutting toolbox in Matlab includes the function smooth(). By default, smooth applies a moving average filter to the data. You can specify the span, which is the width, in points, of the moving average. Example:
y=sin(2*pi*(0:99)/100)+rand(1,100); % noisy signal
span=5;
yy=smooth(y,span); % smoothed signal
plot(0:99,y,'-r',0:99,yy,'-b')
legend('Raw','Smoothed, span=5')
I am not familiar with the smoothing function you used in R, or the meaning of span for the smoothing routine in R. It could be very similar to Matlab's smooth(), as illustrated above.

Community Treasure Hunt

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

Start Hunting!

Translated by