Clear Filters
Clear Filters

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

1 view (last 30 days)
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?

Answers (1)

William Rose
William Rose on 8 May 2024
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!