半对数图(半代数)在MATLAB中忽略零

时间:2016-09-08 18:53:39

标签: matlab

我有一个包含三列的CSV文件。我基本上想要读取前两列,然后绘制带有值的semilogy图。我有以下代码段:

filename = 'ber.csv';
M = csvread(filename, 1)

close all
figure 
semilogy(M(:,1), M(:,2), 'sb-', 'LineWidth', 2);
grid on
axis([0 25 10^-3 10^0])
xlabel('traceback depth, TB')
ylabel('Bit Error Rate');
title('BER with Viterbi decoding')
toc

上面的代码段,为我绘制了类似的内容:

enter image description here

问题是它在x轴上停在24左右,而我有30个值。最后10个值可以在下面看到(为简洁起见省略):

   20.0000    0.0807        
   21.0000    0.0443    
   22.0000    0.0312        
   23.0000    0.0078        
   24.0000    0.0052        
   25.0000         0        
   26.0000         0         
   27.0000         0         
   28.0000         0         
   29.0000         0         
   30.0000         0    

我希望情节能够持续到30,并且它要击中x轴,而剩下的值从25到30,只是0.任何想法如何实现这一点?

1 个答案:

答案 0 :(得分:1)

你这里没有编码问题,你有数学问题。如果在MATLAB中输入SELECT A.Col1 ,B.Col1 ,LEN(A.Col1) ,CASE WHEN A.Col1=LEFT(B.Col1,LEN(A.Col1)) THEN 'Start with the same digits' ELSE '' END FROM @tblA AS A INNER JOIN @tblB AS B ON A.Col1=LEFT(B.Col1,LEN(A.Col1)) ,您将得到答案log(0)。 MATLAB不知道如何在有限的屏幕上绘制-Inf

您说您希望您的线条击中x轴,但请注意轴的底部是0.001。如果x轴向下移动到0.00001怎么办?你不会再击中轴了。在显示数据时,您需要仔细考虑您要传达的内容。

如果你只想向下延伸到轴,最简单的"你可以做的就是尝试向下推断到x轴并在那里添加一个点并绘制它。

-Inf

一些重要的警告是,这是非常静态的,所以如果你改变你的轴限制,你的新点就不会再击中轴了。要使其工作,需要为轴设置回调函数。此外,这应该工作,因为您的样本数据在如何变为零时非常单调。如果您的数据完全振荡,则插值将失败。我也只对您发布的数据子集进行了测试。由于插值是使用数据"侧向",为了到达x轴,如果数据中有噪声使得y轴不再单调,你可能还需要做一些调整只能在接近零的地方选择1或2个点,而不是像我一样懒惰地粘贴整个矢量。

使用您在上面发布的数据:

figure 
h = semilogy(M(:,1), M(:,2), 'sb-', 'LineWidth', 2);
grid on
axis([0 25 10^-3 10^0])
axis([0 25 10^-3 10^0])
xlabel('traceback depth, TB')
ylabel('Bit Error Rate');
title('BER with Viterbi decoding')

% Get the y limits
y_limits = ylim;

% Find the bottom x-axis
bottom_y_limit = min(y_limits);

% interp1 doesn't like repeated values so throw out the extra zeros
first_zero = find(M(:,2) == 0, 1, 'first');

% Figure out where your plot hits the x-axis.    
new_point = interp1(M(1:first_zero, 2), M(1:first_zero, 1), bottom_y_limit, 'pchip');

% Insert the new point into your data
Mnew = sortrows([M; new_point, bottom_y_limit]);

% Replace the data vector in the plot with the new data that includes the extra point
set(h, 'XData', Mnew(:, 1), 'YData', Mnew(:, 2));

我得到的输出是: Output plot