锁相放大器,数字锁相放大器.C和python版的源代码

本文介绍了数字锁相放大器的工作原理,利用数字信号处理技术从高噪声环境中提取特定频率信号。通过Python和C语言示例展示了如何使用乘法和积分操作实现算法,以及如何在实际信号中应用该技术获取幅度和相位信息。
摘要由CSDN通过智能技术生成

数字锁相放大器.
锁相放大器, 它是一种可以从高噪声环境中提取出特定频率信号的放大器,工作原理主要是利用正弦函数的正交性进行信号的相位检测和幅值测量。如果你对锁相放大器感兴趣,我可以给你更详细的解释。

数字锁相放大器是利用软件算法来实现提取特定频率信号的。这种算法通常使用数字信号处理技术,如快速傅里叶变换(FFT)或数字滤波器,来分析和提取输入信号中的特定频率成分。与传统的模拟锁相放大器相比,数字锁相放大器具有更高的灵活性、精度和稳定性。
以下是一个简单的数字锁相放大器算法的示例,经过验证,输出结果正确.

这个算法使用了乘法和积分操作来提取输入信号中与参考频率相对应的幅度和相位信息。你可以将你的信号数据传递给这个函数,并指定参考频率、采样率和积分时间来获得解调后的幅度和相位。

请注意,这只是一个简单的示例算法,实际应用中可能需要进行更多的优化和调整。此外,数字锁相放大器还有许多其他功能和参数设置,你可以根据具体需求进行进一步的探索和研究。

希望这个示例能帮助你理解数字锁相放大器的基本原理和算法实现!如果你还有其他问题或需要进一步的帮助,请随时告诉我。
使用Python语言实现:

import numpy as np

def digital_lock_in_amplifier(signal, reference_frequency, sampling_rate, integration_time):
    '''
        数字锁相放大器
        @signal 原始信号
        @reference_frequency 参考频率
        @sampling_rate 采样率
        @integration_time 积分时间
    '''
    # 计算参考信号的相位
    reference_phase = 2 * np.pi * reference_frequency * np.arange(len(signal)) / sampling_rate

    # 生成参考信号的正弦和余弦分量
    reference_sin = np.sin(reference_phase)
    reference_cos = np.cos(reference_phase)

    # 将输入信号与参考信号的正弦和余弦分量相乘
    multiplied_sin = signal * reference_sin
    multiplied_cos = signal * reference_cos

    # 对乘积进行积分,得到解调后的信号
    demodulated_sin = np.mean(multiplied_sin) * integration_time
    demodulated_cos = np.mean(multiplied_cos) * integration_time

    # 计算解调后信号的幅度和相位
    amplitude = np.sqrt(demodulated_sin**2 + demodulated_cos**2)
    phase = np.arctan2(demodulated_sin, demodulated_cos)

    return amplitude, phase





import numpy as np
import matplotlib.pyplot as plt

# 设置信号参数
sampling_rate = 1000  # 采样率(Hz)
duration = 1  # 信号持续时间(秒)
frequencies = [10, 60, 100]  # 信号中的频率成分(Hz)
amplitudes = [1, 0.1, 0.25]  # 对应频率成分的幅度
phases = [0, np.pi/4, np.pi/2]  # 对应频率成分的相位(弧度)
print("phases",np.pi/4)
# 生成时间轴
t = np.arange(0, duration, 1/sampling_rate)

# 初始化信号
signal = 0

# 叠加各个频率成分
for freq, amp, phase in zip(frequencies, amplitudes, phases):
    # 生成对应频率的正弦波
    component = amp * np.sin(2 * np.pi * freq * t + phase)
    # 叠加到总信号上
    signal += component

#把signal保存成csv文件
np.savetxt('signal1.csv', signal, delimiter=',')
#把signal保存成csv文件
# signal = signal.tolist()
# signal = np.array(signal)
# signal = signal.reshape(-1, 1)
# signal = pd.DataFrame(signal)
# signal.to_csv('signal.csv', index=False, header=False)
 

# 绘制信号波形图
plt.plot(t, signal)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.title('Test Signal')
plt.grid(True)
plt.show()

amplitude, phase = digital_lock_in_amplifier(signal,60,1000,1)
print("振幅",amplitude * 2)
print("相位",phase)

注意,相位是以弧度为单位的,如果你需要以角度为单位,你可以使用phase * (180.0 / np.pi) 进行转换

下面是C语言版本的, 此版本还未验证. 应该是正确的.

#include <stdio.h>
#include <math.h>
#include <complex.h>

typedef double complex cmpx;

// 数字锁相放大器算法函数
// 输入:signal - 输入信号数组
//       freq - 锁相放大器的参考频率
//       sampling_rate - 采样率
//       n - 信号数组的长度
// 输出:amplitude - 放大后的信号幅度
//       phase - 放大后的信号相位(弧度制)
void digital_lock_in_amplifier(cmpx *signal, double freq, double sampling_rate, int n, double *amplitude, double *phase) {
    // 计算参考信号的复数形式
    cmpx ref_signal = 0;
    for (int i = 0; i < n; i++) {
        double time = (double)i / sampling_rate;
        ref_signal += cos(2 * M_PI * freq * time) + sin(2 * M_PI * freq * time) * I;
    }
    ref_signal /= n; // 平均化参考信号

    // 计算输入信号与参考信号的乘积并求和
    cmpx product_sum = 0;
    for (int i = 0; i < n; i++) {
        product_sum += signal[i] * conj(ref_signal);
    }

    // 计算幅度和相位
    *amplitude = cabs(product_sum); // 幅度
    *phase = carg(product_sum);     // 相位(弧度制)
}

int main() {
    double freq = 50.0;         // 参考频率(Hz)
    double sampling_rate = 1000.0; // 采样率(Hz)
    int n = 1000;               // 信号长度
    double amplitude, phase;

    // 分配信号数组
    cmpx *signal = (cmpx *)malloc(n * sizeof(cmpx));

    // 填充信号数组(这里用正弦波作为示例)
    for (int i = 0; i < n; i++) {
        signal[i] = cos(2 * M_PI * freq * (i / sampling_rate)) + sin(2 * M_PI * freq * (i / sampling_rate)) * I;
    }

    // 调用数字锁相放大器算法函数
    digital_lock_in_amplifier(signal, freq, sampling_rate, n, &amplitude, &phase);

    // 输出结果
    printf("The amplitude of the signal at the reference frequency is: %f\n", amplitude);
    printf("The phase of the signal at the reference frequency is: %f radians\n", phase);

    // 将相位转换为角度(如果需要)
    double phase_degrees = phase * (180.0 / M_PI);
    printf("The phase of the signal at the reference frequency is: %f degrees\n", phase_degrees);

    // 释放内存
    free(signal);

    return 0;
}


在这个代码中,digital_lock_in_amplifier函数现在接受两个额外的指针参数amplitude和phase,用于存储计算出的幅度和相位。cabs函数用于计算复数的幅度,而carg函数用于计算复数的相位角(以弧度为单位)。在main函数中,我们调用digital_lock_in_amplifier函数并打印出计算出的幅度和相位。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值