发现两个向量之间的最佳/缩放/移位 [英] finding the best/ scale/shift between two vectors

查看:139
本文介绍了发现两个向量之间的最佳/缩放/移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个向量重新presents一个函数f(x),和另一矢量f(一个* X + B),即f的缩放和移形式(x)的。我想找到最好的*规模和转移的因素。

I have two vectors that represents a function f(x), and another vector f(a*x+b) i.e. a scaled and shifted version of f(x). I would like to find the best* scale and shift factors.

*最好 - 通过最小二乘误差,最大似然等手段

*best - by means of least squares error , maximum likelihood, etc.

什么想法?

例如:

f1 = [0;0.450541598502498;0.0838213779969326;0.228976968716819;0.91333736150167;0.152378018969223;0.825816977489547;0.538342435260057;0.996134716626885;0.0781755287531837;0.442678269775446;0];
f2 = [-0.029171964726699;-0.0278570165494982;0.0331454732535324;0.187656956432487;0.358856370923984;0.449974662483267;0.391341738643094;0.244800719791534;0.111797007617227;0.0721767235173722;0.0854437239807415;0.143888234591602;0.251750993723227;0.478953530572365;0.748209818420035;0.908044924557262;0.811960826711455;0.512568916956487;0.22669198638799;0.168136111568694;0.365578085161896;0.644996661336714;0.823562159983554;0.792812945867018;0.656803251999341;0.545799498053254;0.587013303815021;0.777464637372241;0.962722388208354;0.980537136457874;0.734416947254272;0.375435649393553;0.106489547770962;0.0892376361668696;0.242467741982851;0.40610516900965;0.427497319032133;0.301874099075184;0.128396341665384;0.00246347624097456;-0.0322120242872125]

*注意,F(X)可能是不可逆...

*note that f(x) may be irreversible...

谢谢

阿辖

推荐答案

下面是一个简单的,有效的,但也许有些幼稚的做法。

Here's a simple, effective, but perhaps somewhat naive approach.

首先确保你通过这两个功能使得一个通用的插补器。这样,你可以在给定的数据点之间评估两种功能。我用了三次样条插值,因为这似乎是一般足以让你提供的(并且不需要额外的工具箱),光滑函数的类型

First make sure you make a generic interpolator through both functions. That way you can evaluate both functions in between the given data points. I used a cubic-splines interpolator, since that seems general enough for the type of smooth functions you provided (and does not require additional toolboxes).

然后你评估源功能(原始)在大量的点。还使用这个号码作为一个内联函数的参数,即作为输入 X ,其中

Then you evaluate the source function ("original") at a large number of points. Use this number also as a parameter in an inline function, that takes as input X, where

X = [a b] 

(如 AX + B )。对于任何输入 X ,这个内联函数会计算

(as in ax+b). For any input X, this inline function will compute

  1. 目标函数在相同的X位置的函数值,但规模和抵消 B ,分别为。

的结果函数值之间的平方差,和先前计算的源功能的那些的总和。

The sum of the squared-differences between the resulting function values, and the ones of the source function you computed earlier.

使用的 fminsearch 此内联函数的一些初步估计(您已经在视觉上或通过自动方式获得的一个)。对于您提供的例子,我用一些随机的,这都收敛到接近最佳配合。

Use this inline function in fminsearch with some initial estimate (one that you have obtained visually or by via automatic means). For the example you provided, I used a few random ones, which all converged to near-optimal fits.

以上所有的code的:

All of the above in code:

function s = findScaleOffset

    %% initialize

    f2 = [0;0.450541598502498;0.0838213779969326;0.228976968716819;0.91333736150167;0.152378018969223;0.825816977489547;0.538342435260057;0.996134716626885;0.0781755287531837;0.442678269775446;0];
    f1 = [-0.029171964726699;-0.0278570165494982;0.0331454732535324;0.187656956432487;0.358856370923984;0.449974662483267;0.391341738643094;0.244800719791534;0.111797007617227;0.0721767235173722;0.0854437239807415;0.143888234591602;0.251750993723227;0.478953530572365;0.748209818420035;0.908044924557262;0.811960826711455;0.512568916956487;0.22669198638799;0.168136111568694;0.365578085161896;0.644996661336714;0.823562159983554;0.792812945867018;0.656803251999341;0.545799498053254;0.587013303815021;0.777464637372241;0.962722388208354;0.980537136457874;0.734416947254272;0.375435649393553;0.106489547770962;0.0892376361668696;0.242467741982851;0.40610516900965;0.427497319032133;0.301874099075184;0.128396341665384;0.00246347624097456;-0.0322120242872125];

    figure(1), clf, hold on

    h(1) = subplot(2,1,1); hold on
    plot(f1);
    legend('Original')

    h(2) = subplot(2,1,2); hold on
    plot(f2);

    linkaxes(h)
    axis([0 max(length(f1),length(f2)), min(min(f1),min(f2)),max(max(f1),max(f2))])


    %% make cubic interpolators and test points

    pp1 = spline(1:numel(f1), f1);
    pp2 = spline(1:numel(f2), f2);

    maxX = max(numel(f1), numel(f2));
    N  = 100 * maxX;

    x2 = linspace(1, maxX, N);
    y1 = ppval(pp1, x2);

    %% search for parameters

    s = fminsearch(@(X) sum( (y1 - ppval(pp2,X(1)*x2+X(2))).^2 ), [0 0])

    %% plot results

    y2 = ppval( pp2, s(1)*x2+s(2));

    figure(1), hold on
    subplot(2,1,2), hold on    
    plot(x2,y2, 'r')
    legend('before', 'after')



end

结果:

s =
2.886234493867320e-001    3.734482822175923e-001

请注意,这个计算的相对的从你产生的数据的一个转换。倒车的数字:

Note that this computes the opposite transformation from the one you generated the data with. Reversing the numbers:

>> 1/s(1) 
ans =    
    3.464721948700991e+000   % seems pretty decent 
>> -s(2)
ans = 
    -3.734482822175923e-001  % hmmm...rather different from 7/11!

(我不知道关于您所提供的7/11值;用你给做一个阴谋的结果的精确值的不准确的逼近源函数...你肯定的7/11?)

(I'm not sure about the 7/11 value you provided; using the exact values you gave to make a plot results in a less accurate approximation to the source function...Are you sure about the 7/11?)

精确度可以通过提高或者

Accuracy can be improved by either

  1. 使用不同的优化器( fmincon fminunc 等)
  2. optimset ,要求从 fminsearch 更高的精度
  3. 在两个 F1有更多的采样点 F2 ,提高了插值的质量
  4. 使用更好的初始估计值
  1. using a different optimizer (fmincon, fminunc, etc.)
  2. demanding a higher accuracy from fminsearch through optimset
  3. having more sample points in both f1 and f2 to improve the quality of the interpolations
  4. Using a better initial estimate

无论如何,这种做法是pretty的一般,并给出很好的结果。它也不需要工具箱。

Anyway, this approach is pretty general and gives nice results. It also requires no toolboxes.

它有一个主要的缺点,但 - 发现可能不是解决方案的全局优化的,例如,这种方法的结果的质量可能是你提供的初步估计相当敏感。所以,总是让一个(差)的情节,以确保最终的解决方案是正确的,或者如果你有大量的这样的事情要做,计算某种品质因素,导致您决定重新开始与不同的优化初步估计。

It has one major drawback though -- the solution found may not be the global optimizer, e.g., the quality of the outcomes of this method could be quite sensitive to the initial estimate you provide. So, always make a (difference) plot to make sure the final solution is accurate, or if you have a large number of such things to do, compute some sort of quality factor upon which you decide to re-start the optimization with a different initial estimate.

有当然很可能使用了傅立叶+梅林变换的结果(所建议的下面chaohuang)作为初始估计这种方法。这可能是矫枉过正,为您提供简单的例子,但我可以很容易想象的情况下,这可能确实是非常有用的。

It is of course very possible to use the results of the Fourier+Mellin transforms (as suggested by chaohuang below) as an initial estimate to this method. That might be overkill for the simple example you provide, but I can easily imagine situations where this could indeed be very useful.

这篇关于发现两个向量之间的最佳/缩放/移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆