Periodic Signal

描述

Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling.

One day, the device fell on the ground accidentally. Profess X wanted to check whether the device can still work properly. So he ran another n Hz sampling to the fallen device and got B0 ... Bn-1.

To compare two periodic signals, Profess X define the DIFFERENCE of signal A and B as follow:

You may assume that two signals are the same if their DIFFERENCE is small enough. 
Profess X is too busy to calculate this value. So the calculation is on you.

输入

The first line contains a single integer T, indicating the number of test cases.

In each test case, the first line contains an integer n. The second line contains n integers, A0 ... An-1. The third line contains n integers, B0 ... Bn-1.

T≤40 including several small test cases and no more than 4 large test cases.

For small test cases, 0<n≤6⋅103.

For large test cases, 0<n≤6⋅104.

For all test cases, 0≤Ai,Bi<220.

输出

For each test case, print the answer in a single line.

样例输入
2
9
3 0 1 4 1 5 9 2 6
5 3 5 8 9 7 9 3 2
5
1 2 3 4 5
2 3 4 5 1
样例输出
80

0

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread(ch) freopen(ch,"r",stdin) #define fwrite(ch) freopen(ch,"w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const double eps = 1e-3; const int maxn = 61234; struct Line {     LL l,r,val;     bool operator <(const struct Line a)const     {         return l < a.l;     } } ln[maxn]; LL bit[maxn]; LL st,en,t; int n,m,tp; LL to[maxn]; map <LL,int> mp; int id,iid; int Lowbit(int x) {     return x&(-x); } void Add(int x,LL ad) {     x++;     while(x <= id+1)     {         bit[x] += ad;         x += Lowbit(x);     } } LL Sum(int x) {     LL ans = 0;     x++;     while(x)     {         ans += bit[x];         x -= Lowbit(x);     }     return ans; } int Search(LL x) {     int l,r;     l = 0,r = id;     int ans = -1;     while(l <= r)     {         int mid = (l+r)>>1;         if(to[mid] <= x)         {             ans = mid;             l = mid+1;         }         else r = mid-1;     }     return ans; } LL solve() {     LL mx = 0;     memset(bit,0,sizeof(bit));     sort(ln,ln+tp);     int l,r;     l = 0,r = 0;     for(int i = 0; i <= id; ++i)     {         int rr = Search(to[i]+t);         while(r < tp && mp[ln[r].l] <= rr)         {             Add(mp[ln[r].r],ln[r].val);             ++r;         }         while(l < tp && mp[ln[l].l] < i)         {             Add(mp[ln[l].r],-ln[l].val);             ++l;         }         mx = max(mx,Sum(rr)); //        printf("%d,%d %lld\n",i,rr,mx); //        printf("%d %d\n",l,r-1);     }     return mx; } int main() {     LL t2,x,y,val,sum;     while(~scanf("%lld%lld",&t,&t2))     {         mp.clear();         iid = 0;         scanf("%lld",&st);         en = st+t2;         scanf("%d%d",&n,&m);         tp = 0;         sum = 0;         for(int i = 0; i < n; ++i)         {             scanf("%lld%lld%lld",&x,&y,&val);             x += y;             if(st <= x && x <= en)             {                 ln[tp].l = x+y;                 ln[tp].r = (en-x)/(2*y)*(2*y)+ln[tp].l;                 ln[tp++].val = val;                 sum += val;                 to[iid++] = ln[tp-1].l;                 to[iid++] = ln[tp-1].r; //                printf("%lld %lld\n",ln[tp-1].l,ln[tp-1].r);             }         }         for(int i = 0; i < m; ++i)         {             scanf("%lld%lld%lld",&x,&y,&val);             ln[tp].l = x+y;             if(st <= x+2*y && x+2*y <= en)             {                 ln[tp].r = (en-x)/(2*y)*(2*y)+ln[tp].l;             }             else ln[tp].r = x+y;             ln[tp++].val = val;             sum += val;             to[iid++] = ln[tp-1].l;             to[iid++] = ln[tp-1].r;        }         sort(to,to+iid);         id = -1;         for(int i = 0; i < iid; ++i)         {             if(!i || to[i] != to[i-1])             {                 ++id;                 to[id] = to[i];                 mp[to[i]] = id;             }         }         printf("%lld\n",sum-solve());     }     return 0; }

% Signal parameter settings fs = 1000; % Sampling frequency (Hz) t_start = 0; % Start time (s) t_end = 200; % End time (s) t = t_start:1/fs:t_end; % Time vector % Generate four test signals x1 = 0.5 * sin(2*pi*1.2*t) + 0.3*sin(2*pi*2.4*t) + 0.2 * sin(2*pi * 3.6*t); % Multi-tone periodic signal x2 = 2*cos(2*pi*0.5*t) + 1.5*cos(2*pi*0.8*t) + cos(2*pi*1.1*t); % Multi-tone periodic signal x3 = 3*exp(-0.5*t).*sin(2*pi * 5*t); % Exponentially damped sine wave x4 = sin(2*pi*(10*t + 5*t.^2)); % Linear chirp (non-periodic) % Periodicity detection parameters signals = {x1, x2, x3, x4}; signal_names = {'x1', 'x2', 'x3', 'x4'}; auto_corr_threshold = 0.8; % Minimum autocorrelation peak threshold min_period = 0.1; % Minimum allowed period (seconds) max_period = 50; % Maximum allowed period (seconds) % Process each signal for sig_idx = 1:4 current_signal = signals{sig_idx}; N = length(current_signal); % Compute normalized autocorrelation max_lag_samples = min(floor(N/2), max_period * fs); % Maximum lag in samples [auto_corr, lags] = xcorr(current_signal, max_lag_samples, 'coeff'); % Keep only non-negative lags auto_corr = auto_corr(lags >= 0); lags = lags(lags >= 0); % Skip zero-lag point (always 1.0) auto_corr_nonzero = auto_corr(2:end); lags_nonzero = lags(2:end); % Initialize detection flags is_periodic = false; period_samples = 0; % Find significant peaks in autocorrelation for k = 2:length(auto_corr_nonzero)-1 current_lag = lags_nonzero(k); current_corr = auto_corr_nonzero(k); lag_seconds = current_lag / fs; % Convert to seconds % Apply period constraints if lag_seconds < min_period || lag_seconds > max_period continue; end % Detect local maxima above threshold if current_corr > auto_corr_threshold && ... current_corr > auto_corr_nonzero(k-1) && ... current_corr > auto_corr_nonzero(k+1) % ============================================= % Exponential Decay Detection (Triple Criteria) % ============================================= % 1. Signal energy decay analysis segment1 = current_signal(1:floor(N/3)); % First 1/3 of signal segment2 = current_signal(floor(2*N/3):end); % Last 1/3 of signal energy1 = sum(segment1.^2); energy2 = sum(segment2.^2); energy_decay_ratio = (energy1 - energy2) / energy1; % 2. Autocorrelation decay slope analysis idx_range = 1:find(lags == current_lag, 1); % Indices from 0 to current lag corr_values = auto_corr(idx_range); time_points = lags(idx_range) / fs; % Convert to seconds % Linear regression to compute slope n_points = length(time_points); mean_time = mean(time_points); mean_corr = mean(corr_values); numerator = 0; denominator = 0; for j = 1:n_points numerator = numerator + (time_points(j) - mean_time) * (corr_values(j) - mean_corr); denominator = denominator + (time_points(j) - mean_time)^2; end if denominator ~= 0 decay_slope = numerator / denominator; else decay_slope = 0; end % 3. Harmonic decay analysis harmonic_decay_avg = 0; valid_harmonics = 0; prev_peak = current_corr; % Check first 3 harmonics for harmonic = 2:3 harmonic_lag = harmonic * current_lag; if harmonic_lag > length(auto_corr) - 1 break; end % Get harmonic correlation value harmonic_corr = auto_corr(harmonic_lag + 1); % +1 for MATLAB indexing % Calculate decay between consecutive harmonics if harmonic_corr > 0.1 && prev_peak > 0.1 harmonic_decay = (prev_peak - harmonic_corr) / prev_peak; harmonic_decay_avg = harmonic_decay_avg + harmonic_decay; valid_harmonics = valid_harmonics + 1; end prev_peak = harmonic_corr; end % Compute average harmonic decay if valid_harmonics > 0 harmonic_decay_avg = harmonic_decay_avg / valid_harmonics; end % ============================================= % Periodicity Decision Logic % ============================================= if energy_decay_ratio > 0.5 || ... % Significant energy decay decay_slope < -0.05 || ... % Strong negative correlation slope harmonic_decay_avg > 0.2 % Significant harmonic decay is_periodic = false; % Non-periodic (decaying signal) else is_periodic = true; % Periodic signal period_samples = current_lag; break; % Exit after first valid period detection end end end % Display results if is_periodic period_seconds = period_samples / fs; fprintf('%s: Periodic signal, period = %.4f seconds\n', signal_names{sig_idx}, period_seconds); else fprintf('%s: Non-periodic signal\n', signal_names{sig_idx}); end end 请写一段matlab代码将上述代码的输出结果存入一个表格中 Energy and Power:Calculate signal energy: \(E = \int |x(t)|^2 dt\)Calculate average power: \(P = \frac{1}{T} \int |x(t)|^2 dt\)Classify as energy signal, power signal, or neitherEven/Odd Symmetry:Check if \(x(t) = x(-t)\) [even]Check if \(x(t) = -x(-t)\) [odd]Or neither/combination Deliverable: Create a table summarizing all properties for each signal 此外,这一表格还应按以上要求存有x1到x4的能量与功率,信号类型,奇偶性
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值