问题描述
下面是我的情节代码.如何使绘图更平滑.
Below is my code for the plot. How to make the plot more smoother.
len1 = [25, 250, 500, 750, 1000];
for k1 = 1:length(len1)
standard_deviation1(k1) = std(resdphs(1:5000, len1(k1)));
end
f10 = [110, 100, 90, 80, 70];
figure(3),plot(f10, standard_deviation1);xlabel('frequency'); ylabel('standarddev');
网格
推荐答案
如评论中所述,您可以首先尝试应用移动平均值到您的数据,这会将局部平滑应用于数据中的重叠窗口.但是,要使此方法成功,必须具有更高的点密度才能实现良好的平滑.目前,您的地块只有几个点,均匀地间隔为500个单位,因此移动平均值会极大地改变地块的外观.我会尽快给您展示一个例子.
As stated in the comments, you can first try to apply a moving average to your data which applies local smoothing to overlapping windows in your data. However, for this to be successful, you must have a higher point density to achieve good smoothing. Currently, your plot only has a few points uniformly spaced at 500 units and so moving average will significantly alter the way the plot looks. I'll show you an example soon.
让我们回到手头的方法.首先,在每个点之间应用线性插值以获得更高的点密度.应用线性插值后,可以使用 conv
.但是,将要发生的是,在关键点之间将存在不代表您的问题的人工数据.我还要提及的是,此图是出于美学目的,关键点之间的数据不应用于任何关键决策.
Let's get back to the method at hand. First, apply linear interpolation between each of the points to get a higher point density. After you apply linear interpolation, you can apply the moving average operation with conv
. However, what will happen is that in between your keypoints will exist artificial data that isn't representative of your problem. I'd also like to mention that this plot is for aesthetic purposes and the data in between the keypoints should not be used for any critical decisions.
如果只想绘制点,请考虑不使用plot
并使用 stem
.无论如何,都可以使用 interp1
作为内插的基本方法在关键点之间.完成此操作后,您可以通过卷积应用移动平均值-具体来说,请使用具有少量滤波器抽头且均等权重的内核.像5抽头窗口或7抽头窗口之类的东西就足够了.
If you simply want to plot the points, consider not using plot
and using stem
instead. In any case, use interp1
as the base method for interpolating in between the keypoints. Once you do that, you can apply a moving average by convolution - specifically, use a kernel that has a small amount of filter taps that are all equally weighted. Something like a 5-tap window or 7-tap window may suffice.
使用您在上面声明的变量:
Using the variables that you declared above:
%// Specify number of total points
num_points = 300;
%// Specify moving average window
move_size = 7;
%// Specify interpolated y coordinates
xpts = linspace(min(f10), max(f10), num_points);
out = interp1(f10, standard_deviation1, xpts, 'linear');
%// Apply moving average
kernel = (1/move_size)*(ones(1,move_size));
out_smooth = conv(out, kernel, 'same');
%// Also apply moving average on the raw data itself for demonstration
out_smooth_raw = conv(standard_deviation1, kernel, 'same');
%// Plot everything
plot(f10, standard_deviation1, f10, out_smooth_raw, 'x-', xpts, out_smooth);
legend('Original Data', 'Smoothed Data - Raw', 'Smoothed Data - Interpolated');
让我们使用一些示例数据来做到这一点:
Let's do this with some example data:
f10 = 0 : 500 : 5000;
rng(123); %// Set seed for reproducibility
standard_deviation1 = rand(1,numel(f10));
使用上面的数据和上面的代码,我们得到以下图:
Using the above data and with the above code, we get this plot:
如您所见,由于分辨率的原因,在没有插值的情况下对数据应用移动平均会极大地改变数据.如果先应用插值,然后应用移动平均,您会发现在平滑角的情况下可以更好地表示原始数据.请记住,在平滑结果的开始和结尾处的数据将毫无意义,因为您将对填充了零的窗口的移动平均值进行填充以使计算有效.
As you can see, applying a moving average on your data without interpolation significantly alters the data because of the resolution. If you apply interpolation first, then apply a moving average, you will see that you get a somewhat better representation of your original data with the corners smoothed. Bear in mind that the data at the beginning and at the end of the smoothened result will be meaningless as you would be taking the moving average of windows with zeros padded into the data to allow the calculations to work.
这篇关于平滑曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!