Omega Overview

本文介绍Xapian和Omega两个组件如何合作提供索引和搜索功能。Xapian负责存储信息及搜索,而Omega则将不同格式的数据转换为Xapian可索引的格式。文中还演示了如何使用Omega建立索引数据库并查询。

Xapian 和 Omega 是独立的两个组件,旨在相互协作来提供索引和搜索功能。Xapian 组件提供了核心的数据库功能(存储信息)以及搜索和检索系统来查找单词和单词组合。

Omega 组件提供了一些工具来将不同格式的信息转化并解析成 Xapian 需要的原始格式以便它可以被索引。Omega 使用了多种工具,比如 pdftotext,然后再提交转化并过滤了的基于文本的信息,以便文档的索引和结构可以被标识并存储在 Xapian 数据库。Omega 是 Xapian 下载的一部分。

先来把Omega小用一把,让你对它有个直观的感受,然后我们再来详细介绍Omega。

建立索引数据库:

$ omindex --db db1 --url may9 /home/orisun/master/chsegtxt

db1是你自己取的数据库名,may9是你自己定义的索引信息的URL, /home/orisun/master/chsegtxt下是你要建立索引的文件。

执行后会在当前路径下生成一个文件夹db1,那may9在哪儿呢?马上就看到它了。

查询数据库:

quest --db=db1 经济

从数据库db1中检索含有“经济“的文档。返回的内容开如:

Parsed Query: Xapian::Query(Z经济:(pos=1))

MSet:

67 [100%]

url=may9/f00153.txt

sample=2011 年 05 月 06 日 周五 出版 * * * * * *  设置 我 的 评论 站 内 信 登出 登入 新手指南 免费注册 您 的 反馈 [ keys ] * # 文章 * o 日期  会员 信息 FT 中文网 iPad 应用程序 全面 升级 ! 复旦 蓝墨水 活动 - 与 资深 HR 及 高管 会面 “ 世界 因 你 ...

type=text/plain

modtime=1304820622

size=29984

如果想要对一个 web 站点进行爬行并索引,可以使用 htdig2omega,它能接受一个 URL 并搜索整个 web 站点。

构建了初始的数据库后,就能向该数据库添加更多的文档和目录了,不过,为了这个目的,应该使用不同 URL 目录,以便能更为显式地在索引内定位文档。您应该使用 -p 选项来确保现有的文档没有在添加过程中被删除:

$ omindex -p --db info --url documents /mnt/data0/Documents


% 清空工作区和命令窗口 clear; clc; close all; % 读取CSV文件(请替换为实际文件路径) full_data = readmatrix('ukf1.csv'); time = full_data(:, 1); speed = full_data(:, 3); % 确保时间序列严格递增 [time, sort_idx] = sort(time); speed = speed(sort_idx); % 使用前150个点进行最小二乘正弦拟合(UKF初始化) num_init_points = 150; t_init = time(1:num_init_points); y_init = speed(1:num_init_points); % 使用原始数据初始化 % 初始猜测参数:[A, omega, phi] p0 = [ (0.780+1.045)/2; (1.884+2)/2; 0.0 ]; % 定义拟合函数 fit_func = @(p, t) p(1) * sin(p(2) * t + p(3)) + 2.09 - p(1); % 设置优化选项 options = optimoptions('lsqcurvefit', ... 'Display', 'off', ... 'MaxFunctionEvaluations', 1000, ... 'MaxIterations', 1000, ... 'TolFun', 1e-8, ... 'TolX', 1e-8); lb = [0.68, 1.784, -pi]; % 下限 ub = [1.145, 2.1, pi]; % 上限 % 执行最小二乘拟合 p_fit = lsqcurvefit(fit_func, p0, t_init, y_init, lb, ub, options); % 提取拟合参数 A_fit = p_fit(1); omega_fit = p_fit(2); phi_fit = p_fit(3); v0_fit = 2.09 - p_fit(1); % 转换为 sin(phi) 和 cos(phi) s_phi_fit = sin(phi_fit); c_phi_fit = cos(phi_fit); % 更新初始状态 x0 x0 = [ (0.780+1.045)/2; (1.884+2)/2; phi_fit; ]; % 显示结果 disp('使用最小二乘法拟合的初始参数:'); disp(['A = ', num2str(A_fit)]); disp(['omega = ', num2str(omega_fit)]); disp(['phi = ', num2str(phi_fit)]); disp(['sin(phi) = ', num2str(s_phi_fit)]); disp(['cos(phi) = ', num2str(c_phi_fit)]); disp(['v0 = ', num2str(v0_fit)]); % 初始状态协方差 P0 = 1e-3 * diag([0.001, 0.001, 0.001]); % 初始过程噪声协方差 Q Q = 1e-10 * diag([1, 1, 1]); % 初始观测噪声协方差 R R = 1; % UKF 参数 alpha = 1e-2; beta = 2; kappa = 0; % 创建 UKF 对象 ukf = unscentedKalmanFilter(... 'StateTransitionFcn', @stateTransitionFcn, ... 'MeasurementFcn', @measurementFcn, ... 'State', x0, ... 'StateCovariance', P0, ... 'ProcessNoise', Q, ... 'MeasurementNoise', R, ... 'HasAdditiveMeasurementNoise', true, ... 'Alpha', alpha, ... 'Beta', beta, ... 'Kappa', kappa); % 初始化变量 innovations = []; R_estimates = []; Q_estimates = []; estimated_params = []; fit_speed = []; window_size = 10; % 更新UKF for i = 1:length(time) % 获取当前时间点 current_time = time(i); % 预测 [x_pred, P_pred] = predict(ukf); % 计算新息(残差) y_measured = speed(i); y_pred = measurementFcn(x_pred, current_time); y_innovation = y_measured - y_pred; % 更新观测噪声 R(滑动窗口估计) innovations = [innovations; y_innovation]; if length(innovations) > window_size innovations(1) = []; % 移除旧值 end current_R = max(var(innovations)*1e-1, 1e-8); % 保证不为0 if length(innovations) < window_size current_R = 0.1; end ukf.MeasurementNoise = current_R; R_estimates = [R_estimates; current_R]; % 校正 ukf.State = correct(ukf, y_measured, current_time); % 存储结果 estimated_params(i, :) = ukf.State'; fit_speed(i) = measurementFcn(ukf.State, current_time); end %% 使用UKF滤波后的数据进行最小二乘拟合(每隔50个点拟合一次) % 定义拟合函数 fit_func = @(p, t) p(1) * sin(p(2)*t + p(3)) + 2.09 - p(1); % 初始猜测参数 p0_new = [ (0.780+1.045)/2; (1.884+2)/2; 0 ]; lb = [0.68, 1.784, -pi]; ub = [1.145, 2.1, pi]; options = optimoptions('lsqcurvefit', ... 'Display', 'off', ... 'MaxFunctionEvaluations', 1000, ... 'MaxIterations', 1000, ... 'TolFun', 1e-8, ... 'TolX', 1e-8); % 初始化存储参数 num_fits = 5; window_size = 300; fit_params = zeros(num_fits, 3); fit_times = zeros(num_fits, 1); for i = 1:num_fits start_idx = (i-1)*window_size + 1; end_idx = i*window_size; if end_idx > length(time) end_idx = length(time); end t_fit = time(start_idx:end_idx); y_fit = fit_speed(start_idx:end_idx)'; % UKF滤波后的数据 y_raw = speed(start_idx:end_idx); % 原始数据 % 执行拟合(可放在绘图前或后) p_fit = lsqcurvefit(fit_func, p0_new, t_fit, y_fit, lb, ub, options); % 更新初始猜测参数 p0_new = p_fit; % 保存拟合参数 fit_params(i, :) = p_fit'; fit_times(i) = mean(t_fit); % 绘图:原始 vs UKF滤波 figure; % 新建一个图 plot(t_fit, y_raw, 'b.', 'DisplayName', '原始数据'); hold on; plot(t_fit, y_fit, 'r-', 'LineWidth', 1.5, 'DisplayName', 'UKF滤波数据'); title(sprintf('第 %d 次拟合使用的数据(点 %d 到 %d)', i, start_idx, end_idx)); xlabel('时间 (s)'); ylabel('速度 (rad/s)'); legend('Location', 'best'); grid on; hold off; end % 提取参数 A_fit = fit_params(:, 1); omega_fit = fit_params(:, 2); phi_fit = fit_params(:, 3); %% 绘图:参数变化 figure; subplot(3, 1, 1); plot(fit_times, A_fit, 'o-'); title('振幅 A 的变化(UKF后拟合)'); xlabel('时间 (s)'); ylabel('A'); subplot(3, 1, 2); plot(fit_times, omega_fit, 'o-'); title('角频率 \omega 的变化(UKF后拟合)'); xlabel('时间 (s)'); ylabel('\omega'); subplot(3, 1, 3); plot(fit_times, phi_fit, 'o-'); title('相位 \phi 的变化(UKF后拟合)'); xlabel('时间 (s)'); ylabel('\phi'); sgtitle('基于UKF滤波后数据的最小二乘拟合参数变化'); %% 显示每次拟合结果 disp('每次拟合的参数结果:'); for i = 1:num_fits fprintf('第 %d 次拟合:A=%.4f, omega=%.4f, phi=%.4f\n', i, fit_params(i, :)); end %% 函数定义区域 % 状态转移函数(恒等函数) function x = stateTransitionFcn(x) x = x(:); % 强制为列向量 end % 测量函数 function y = measurementFcn(x, t) A = x(1); omega = x(2); phi = x(3); v0 = 2.09 - A; y = A * sin(omega*t + phi) + v0; end 请在原图完整的原始数据中展现各自使用的ukf数据段
07-26
非常好!你现在的代码已经可以: - 对原始数据进行 UKF 滤波; - 对滤波后的数据分段进行最小二乘拟合; - 绘制每段使用的 **原始数据 vs UKF 滤波数据**。 --- ### ✅ 你现在的目标是: > 在**原始数据的完整图中**,标注出每段用于拟合的 **UKF 数据段**,以便直观看到这些段在整体趋势中的位置。 --- ### ✅ 实现方法: 1. 在 UKF 处理完成后,先绘制原始数据和 UKF 滤波数据的完整图; 2. 然后在每次拟合循环中,使用 `xline` 或 `fill` 标注当前拟合段的起止时间; 3. 所有拟合段标注完成后,形成一张图,展示原始数据 + UKF 数据 + 拟合段位置。 --- ### ✅ 修改后的代码如下: 将这段代码放在 `for i = 1:num_fits` 循环之前,即绘制完完整数据之后: ```matlab % 绘制完整原始数据和UKF滤波数据 figure; plot(time, speed, 'b.', 'DisplayName', '原始数据'); hold on; plot(time, fit_speed, 'r-', 'LineWidth', 1.5, 'DisplayName', 'UKF滤波数据'); xlabel('时间 (s)'); ylabel('速度 (rad/s)'); title('原始数据与UKF滤波数据(全时间段)'); legend('Location', 'best'); grid on; ``` 然后在 `for i = 1:num_fits` 循环中,添加以下代码: ```matlab % 在完整图中高亮当前拟合段 xline(start_idx, '--g', sprintf('第 %d 段起始点', i), 'LabelVerticalAlignment', 'middle'); xline(end_idx, '--k', sprintf('第 %d 段结束点', i), 'LabelVerticalAlignment', 'middle'); % 可选:用矩形标注时间段 ylims = ylim; fill([t_fit(1) t_fit(end) t_fit(end) t_fit(1)], ... [ylims(1) ylims(1) ylims(2) ylims(2)], ... [0.9 0.9 0.9], 'FaceAlpha', 0.3, 'EdgeColor', 'none'); ``` --- ### ✅ 完整插入代码如下: ```matlab % 绘制完整原始数据和UKF滤波数据 figure; plot(time, speed, 'b.', 'DisplayName', '原始数据'); hold on; plot(time, fit_speed, 'r-', 'LineWidth', 1.5, 'DisplayName', 'UKF滤波数据'); xlabel('时间 (s)'); ylabel('速度 (rad/s)'); title('原始数据与UKF滤波数据(全时间段)'); legend('Location', 'best'); grid on; % 在每段拟合时用 xline 标注起止点 for i = 1:num_fits start_idx = (i-1)*window_size + 1; end_idx = i*window_size; if end_idx > length(time) end_idx = length(time); end t_fit = time(start_idx:end_idx); y_fit = fit_speed(start_idx:end_idx); % UKF滤波后的数据 y_raw = speed(start_idx:end_idx); % 原始数据 % 执行拟合(可放在绘图前或后) p_fit = lsqcurvefit(fit_func, p0_new, t_fit, y_fit, lb, ub, options); % 更新初始猜测参数 p0_new = p_fit; % 保存拟合参数 fit_params(i, :) = p_fit'; fit_times(i) = mean(t_fit); % 绘图:原始 vs UKF滤波 figure; % 新建一个图 plot(t_fit, y_raw, 'b.', 'DisplayName', '原始数据'); hold on; plot(t_fit, y_fit, 'r-', 'LineWidth', 1.5, 'DisplayName', 'UKF滤波数据'); title(sprintf('第 %d 次拟合使用的数据(点 %d 到 %d)', i, start_idx, end_idx)); xlabel('时间 (s)'); ylabel('速度 (rad/s)'); legend('Location', 'best'); grid on; hold off; % 在完整图中标注该段 figure(1); % 切换到完整数据图 xline(t_fit(1), '--g', sprintf('第 %d 段起点', i), 'LabelVerticalAlignment', 'middle'); xline(t_fit(end), '--k', sprintf('第 %d 段终点', i), 'LabelVerticalAlignment', 'middle'); % 用浅色矩形标注时间段 ylims = ylim; fill([t_fit(1) t_fit(end) t_fit(end) t_fit(1)], ... [ylims(1) ylims(1) ylims(2) ylims(2)], ... [0.9 0.9 0.9], 'FaceAlpha', 0.3, 'EdgeColor', 'none'); end ``` --- ### ✅ 效果说明: - **图 1:完整原始数据 vs UKF滤波数据** - 用 `xline` 标注每段的起止点 - 用 `fill` 区域填充标注每段范围 - **图 2~6:每段的局部图** - 显示原始数据和UKF滤波数据对比 --- ### ✅ 示例图说明: ``` 原始数据与UKF滤波数据(全时间段) ---------------------------------- | | | 蓝色点:原始速度数据 | | 红色线:UKF滤波后数据 | | 绿色虚线:每段起始点 | | 黑色虚线:每段结束点 | | 浅灰色区域:每段拟合范围 | | | ---------------------------------- ``` --- ### ✅ 补充建议: - 使用 `legend('Location', 'bestoutside')` 避免图例遮挡 - 使用 `sgtitle` 为多个子图添加总标题 - 使用 `saveas(gcf, 'ukf_overview.png')` 保存图像 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值