我在 Matlab 中有以下問題:
我有一個時間序列,如下所示:
size(ts) = (n,2); % with n being the number of samples, the first column is the time, the second the value.
假設我有:
ts(:,1) = [0, 10, 20, 30, 40];
ts(:,2) = [1, 3, 10, 6, 11];
我想對上面的信號重新采樣以獲得不同時間的插值。說:
ts(:,1) = [0, 1, 3, 15, 40];
ts(:,2) = ???
我查看了用于信號處理的 Matlab 函式,但它們都只與各種頻率的常規采樣有關。
是否有內置函式可以為我提供上述資訊,或者我是否必須手動計算每個新所需時間的線性插值?如果是這樣,您是否有建議使用 vecotrized 代碼有效地執行此操作(一個月前剛開始使用 Matlab,所以仍然 100% 輕松地使用這個,并且仍然依賴 for 回圈很多)。
對于一些背景關系,我正在使用串聯的有限差分方案來調查問題。一個 FD 方案的輸出被饋送到下面。由于我的問題的性質,我必須將時間步長從一個 FD 更改為下一個,并且我的時間步長可能是不規則的。
謝謝。
uj5u.com熱心網友回復:
由于您的資料是一維資料,您可以使用interp1執行插值。該代碼將按如下方式作業:
ts = [0, 10, 20, 30, 40; % Time/step number
1, 3, 10, 6, 11]; % Values
resampled_steps = [0, 1, 3, 15, 40]; % Time for which we want resample
resampled_values = interp1(ts(1, :), ts(2, :), resampled_step);
% Put everything in an array to match initial format
ts_resampled = [resampled_steps; resampled_values];
或者你可以濃縮一點,遵循同樣的想法:
ts = [0, 10, 20, 30, 40; % Time/step number
1, 3, 10, 6, 11]; % Values
% Create resample array
ts_resampled = zeros(size(ts));
ts_resampled(1, :) = [0, 1, 3, 15, 40];
% Interpolate
ts_resampled(2, :) = interp1(ts(1, :), ts(2, :), ts_resampled(1, :));
您甚至可以通過將字串傳遞給interp1函式來選擇所需的插值方法。方法在這里列出
請注意,這僅在您使用原始范圍內的時間戳重新采樣時才有效。如果你想讓它們在外面,你必須告訴函式如何使用關鍵字“extra”進行推斷。詳細在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/475410.html
下一篇:Matlab中求和的分支函式
