var _this = this;的理解以及详解

本文通过两个实例详细解析了在jQuery中使用_this与this的关键区别。阐述了在一个代码片段中,this可能代表不同对象,而_this则用于锁定最初对象的引用,确保在嵌套函数中正确引用外部函数的上下文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例一

不知道楼主有没有接触过 jquery里边有一个特别典型的例子能说明用_this的作用
$("#btn").click(function(){
   var _this = this;//这里this和_this都代表了"#btn"这个对象
$(".tr").each(function(){
this;//在这里this代表的是每个遍历到的".tr"对象
_this;//仍代表"#btn"对象
})
})
这种情况就是在一个代码片段里this有可能代表不同的对象,而编码者希望_this代表最初的对象

例二:

$(‘#conten').click(function(){
    //this是被点击的#conten
    var that = this;
    $(‘.conten').each(function(){
    //this是.conten循环中当前的对象
    //that仍然是刚才被点击的#conten
    });
});

 

 
 

%% 参数设置 Fs = 5e9; % 采样率 5 GHz fc = 1e9; % 载波频率 1 GHz bw = 5e6; % 信号带宽 5 MHz duration = 20e-6; % 信号持续时间 20 μs t = 0:1/Fs:duration-1/Fs; %% 生成QPSK调制信号 fprintf('生成QPSK调制信号...\n'); symbol_rate = bw/2; % 符号率 2.5 MHz sps = floor(Fs/symbol_rate); % 每符号采样点数 num_symbols = ceil(length(t)/sps); data = randi([0 3], 1, num_symbols); % QPSK数据 modulated = pskmod(data, 4, pi/4); % QPSK调制 % 升余弦滤波 rolloff = 0.35; filt = rcosdesign(rolloff, 6, sps); baseband = upfirdn(modulated, filt, sps, 1); baseband = baseband(1:length(t)); %% 上变频 fprintf('上变频到RF...\n'); carrier = exp(1i*2*pi*fc*t); rf_signal = real(baseband .* carrier); %% 添加噪声 fprintf('添加噪声...\n'); noise_power = 0.01 * var(rf_signal); rf_signal = rf_signal + sqrt(noise_power)*randn(size(rf_signal)); %% 定点量化 (16位有符号) fprintf('定点量化...\n'); nbits = 16; max_val = 2^(nbits-1)-1; rf_signal_normalized = rf_signal / max(abs(rf_signal)); rf_fixed = round(rf_signal_normalized * max_val); rf_fixed(rf_fixed > max_val) = max_val; rf_fixed(rf_fixed < -max_val) = -max_val; %% 重组为16路并行数据 fprintf('重组为16路并行数据...\n'); num_samples = length(rf_fixed); if mod(num_samples, 16) ~= 0 rf_fixed = [rf_fixed, zeros(1, 16 - mod(num_samples, 16))]; num_samples = length(rf_fixed); end parallel_data = reshape(rf_fixed, 16, []); num_blocks = size(parallel_data, 2); %% ===================== 串行DDC实现 (256倍抽取) ===================== fprintf('\n===== 开始串行DDC处理 (256倍抽取) =====\n'); tic; % 1. 重新设计低通滤波器 (更严格的抗混叠要求) Fs_serial = Fs; % 串行采样率 5 GHz Fpass = 2.5e6; % 通带截止频率 Fstop = 5e6; % 阻带截止频率 (更严格) Apass = 1; % 通带波动(dB) Astop = 80; % 阻带衰减(dB) (更高要求) h_serial_256 = designfilt('lowpassfir', ... 'PassbandFrequency', Fpass/(Fs_serial/2), ... 'StopbandFrequency', Fstop/(Fs_serial/2), ... 'PassbandRipple', Apass, ... 'StopbandAttenuation', Astop, ... 'DesignMethod', 'equiripple'); % 使用等波纹设计获得更高性能 filter_coeffs_serial_256 = h_serial_256.Coefficients; % 2. NCO设置 nco_phase = 0; nco_phase_step = 2*pi*fc/Fs_serial; % 3. 处理信号 serial_mixed = complex(zeros(1, num_samples)); for i = 1:num_samples % NCO混频 nco_carrier = exp(-1i * nco_phase); serial_mixed(i) = double(rf_fixed(i)) * nco_carrier; % 更新NCO相位 nco_phase = mod(nco_phase + nco_phase_step, 2*pi); end % 4. FIR滤波 serial_filtered_256 = filter(filter_coeffs_serial_256, 1, serial_mixed); % 5. 256倍抽取 serial_decimated_256 = serial_filtered_256(1:256:end); serial_time_256 = toc; fprintf('串行DDC(256x)处理完成! 耗时: %.4f 秒\n', serial_time_256); %% ===================== 并行DDC实现 ===================== fprintf('\n===== 开始并行DDC处理 =====\n'); tic; % 1. 参数设置 nco_phase_step = 2*pi*fc/(Fs/16); % 每路相位增量 nco_initial_phase = 2*pi*fc*(0:15)/Fs; % 16路初始相位 % 2. 设计多相滤波器 (使用相同的滤波器规格) Fs_per_channel = Fs/16; % 每路采样率312.5 MHz h_parallel = designfilt('lowpassfir', ... 'PassbandFrequency', Fpass/(Fs_per_channel/2), ... 'StopbandFrequency', Fstop/(Fs_per_channel/2), ... 'PassbandRipple', Apass, ... 'StopbandAttenuation', Astop); filter_coeffs = h_parallel.Coefficients; filter_length = length(filter_coeffs); num_phases = 16; % 3. 多相分解 max_poly_length = ceil(filter_length/num_phases); polyphase_filters = zeros(num_phases, max_poly_length); for k = 1:num_phases branch_coeffs = filter_coeffs(k:num_phases:end); if length(branch_coeffs) < max_poly_length branch_coeffs = [branch_coeffs, zeros(1, max_poly_length - length(branch_coeffs))]; end polyphase_filters(k, :) = branch_coeffs; end % 4. 初始化处理变量 mixed_data = zeros(16, num_blocks); % 5. 处理每个样本块 for block_idx = 1:num_blocks samples = parallel_data(:, block_idx); current_phase = nco_initial_phase + (block_idx-1)*nco_phase_step; nco_carrier = exp(-1i * current_phase); mixed_data(:, block_idx) = double(samples) .* nco_carrier.'; end % 6. 多相滤波抽取 num_decimated = floor(num_blocks/16); parallel_decimated_data = zeros(16, num_decimated); for ch = 1:16 channel_data = mixed_data(ch, :); filtered = filter(polyphase_filters(ch,:), 1, channel_data); parallel_decimated_data(ch, :) = filtered(1:16:16*num_decimated); end % 重组为串行数据 parallel_decimated = reshape(parallel_decimated_data, 1, []); parallel_time = toc; fprintf('并行DDC处理完成! 耗时: %.4f 秒\n', parallel_time); %% ===================== 性能对比 ===================== fprintf('\n===== 性能对比 =====\n'); fprintf('串行DDC(256x)处理时间: %.4f 秒\n', serial_time_256); fprintf('并行DDC处理时间: %.4f 秒\n', parallel_time); fprintf('加速比: %.2f\n', serial_time_256/parallel_time); % 计算信号误差 min_len = min(length(serial_decimated_256), length(parallel_decimated)); error = serial_decimated_256(1:min_len) - parallel_decimated(1:min_len); mse = mean(abs(error).^2); fprintf('均方误差 (MSE): %.4e\n', mse); %% ===================== 频谱分析 ===================== % 确保采样率一致 Fs_decimated = Fs/256; % 19.53125 MHz % 1. 原始信号频谱 figure('Name', '原始信号频谱', 'Position', [50, 50, 800, 400], 'NumberTitle', 'off'); N_fft = 2^nextpow2(num_samples); f_axis = (-N_fft/2:N_fft/2-1)*(Fs/N_fft); spectrum = fftshift(abs(fft(rf_fixed, N_fft))); plot(f_axis/1e9, 20*log10(spectrum/max(spectrum))); title('原始信号频谱 (5 GHz 采样率)'); xlabel('频率 (GHz)'); ylabel('幅度 (dB)'); xlim([0, 2.5]); grid on; % 2. 串行DDC混频后频谱 figure('Name', '串行DDC混频后频谱', 'Position', [100, 100, 800, 400], 'NumberTitle', 'off'); N_fft_mixed = 2^nextpow2(length(serial_mixed)); f_axis_mixed = (-N_fft_mixed/2:N_fft_mixed/2-1)*(Fs/N_fft_mixed); spectrum_mixed = fftshift(abs(fft(serial_mixed, N_fft_mixed))); plot(f_axis_mixed/1e6, 20*log10(spectrum_mixed/max(spectrum_mixed))); title('串行DDC混频后基带信号频谱 (5 GHz 采样率)'); xlabel('频率 (MHz)'); ylabel('幅度 (dB)'); xlim([-20, 20]); grid on; % 3. 串行DDC抽取后频谱 (256倍) figure('Name', '串行DDC(256x)抽取后频谱', 'Position', [150, 150, 800, 400], 'NumberTitle', 'off'); N_fft_decimated = 2^nextpow2(length(serial_decimated_256)); f_axis_decimated = (-N_fft_decimated/2:N_fft_decimated/2-1)*(Fs_decimated/N_fft_decimated); spectrum_decimated = fftshift(abs(fft(serial_decimated_256, N_fft_decimated))); plot(f_axis_decimated/1e6, 20*log10(spectrum_decimated/max(spectrum_decimated))); title(sprintf('串行DDC(256x)抽取后信号频谱 (%.2f MHz 采样率)', Fs_decimated/1e6)); xlabel('频率 (MHz)'); ylabel('幅度 (dB)'); xlim([-5, 5]); grid on; % 4. 并行DDC混频后频谱 figure('Name', '并行DDC混频后频谱', 'Position', [200, 200, 800, 400], 'NumberTitle', 'off'); mixed_serial = reshape(mixed_data, 1, []); N_fft_mixed_parallel = 2^nextpow2(length(mixed_serial)); f_axis_mixed_parallel = (-N_fft_mixed_parallel/2:N_fft_mixed_parallel/2-1)*(Fs/N_fft_mixed_parallel); spectrum_mixed_parallel = fftshift(abs(fft(mixed_serial, N_fft_mixed_parallel))); plot(f_axis_mixed_parallel/1e6, 20*log10(spectrum_mixed_parallel/max(spectrum_mixed_parallel))); title('并行DDC混频后基带信号频谱 (312.5 MHz 采样率)'); xlabel('频率 (MHz)'); ylabel('幅度 (dB)'); xlim([-20, 20]); grid on; % 5. 并行DDC抽取后频谱 figure('Name', '并行DDC抽取后频谱', 'Position', [250, 250, 800, 400], 'NumberTitle', 'off'); N_fft_parallel_decimated = 2^nextpow2(length(parallel_decimated)); f_axis_parallel_decimated = (-N_fft_parallel_decimated/2:N_fft_parallel_decimated/2-1)*(Fs_decimated/N_fft_parallel_decimated); spectrum_parallel_decimated = fftshift(abs(fft(parallel_decimated, N_fft_parallel_decimated))); plot(f_axis_parallel_decimated/1e6, 20*log10(spectrum_parallel_decimated/max(spectrum_parallel_decimated))); title(sprintf('并行DDC抽取后信号频谱 (%.2f MHz 采样率)', Fs_decimated/1e6)); xlabel('频率 (MHz)'); ylabel('幅度 (dB)'); xlim([-5, 5]); grid on; % 6. 输出信号对比 figure('Name', 'DDC输出信号对比 (256x vs 并行)', 'Position', [300, 300, 1000, 600], 'NumberTitle', 'off'); % 时域对比 subplot(2,1,1); plot(real(serial_decimated_256(1:min_len))), hold on; plot(real(parallel_decimated(1:min_len))); title('时域信号对比 (实部)'); xlabel('样本索引'); ylabel('幅度'); legend('串行DDC(256x)', '并行DDC'); grid on; % 频域对比 subplot(2,1,2); plot(f_axis_decimated/1e6, 20*log10(spectrum_decimated/max(spectrum_decimated))), hold on; plot(f_axis_parallel_decimated/1e6, 20*log10(spectrum_parallel_decimated/max(spectrum_parallel_decimated))); title('频域信号对比'); xlabel('频率 (MHz)'); ylabel('幅度 (dB)'); xlim([-5, 5]); legend('串行DDC(256x)', '并行DDC'); grid on; %% ===================== 资源消耗对比 ===================== fprintf('\n===== 资源消耗对比 =====\n'); fprintf('串行DDC(256x):\n'); fprintf(' - 乘法器数量: %d\n', length(filter_coeffs_serial_256)); fprintf(' - 加法器数量: %d\n', length(filter_coeffs_serial_256)-1); fprintf(' - 每输出样本乘法次数: %d\n', length(filter_coeffs_serial_256)); fprintf('\n并行DDC:\n'); fprintf(' - 乘法器总数: %d\n', size(polyphase_filters, 1) * size(polyphase_filters, 2)); fprintf(' - 每通道乘法器数量: %d\n', size(polyphase_filters, 2)); fprintf(' - 每输出样本乘法次数: %d (所有通道总)\n', size(polyphase_filters, 2)); fprintf(' - 实际每输出样本乘法次数: %.2f (按通道数平均)\n', size(polyphase_filters, 2)/16); %% 保存结果 save('ddc_comparison.mat', 'serial_time_256', 'parallel_time', 'mse', ... 'filter_coeffs_serial_256', 'polyphase_filters', 'serial_decimated_256', 'parallel_decimated'); fprintf('\n处理完成! 结果已保存。\n'); 给出这段代码的详细说明,重点说明,信号的采样点数,滤波器的参数设计,预期达到的效果
最新发布
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值