基于Pyqt5的正弦波信号分析界面

基于PyQt5的正弦波信号分析界面

简介

使用PyQt5界面生成一个正弦波 ,可以调整频率、振幅、采样频率、采样时间、还可以混合频率。

对正弦波可进行FFT傅里叶变换STFT短时傅里叶变换分析 ,并能够显示对应的图形。

可当作一个简单的信号处理界面,后续还可以添加IIR滤波器、FIR滤波器等。

复制代码
功能:
1、生成正弦波
2、FFT频域分析
3、STFT频域分析

1、效果图

复制代码
需要主体代码可以联系
"""
@contact: 微信 1257309054
@file: main_windows.py
@time: 2024/3/30 8:40
@author: LDC
"""

2、生成正弦波

示例代码:

python 复制代码
# -*- coding: utf-8 -*-

"""
@contact: 微信 1257309054
@file: main_windows.py
@time: 2024/3/30 8:40
@author: LDC
"""
import numpy as np
import sys
import matplotlib
import pyqtgraph
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator, QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout
from scipy import signal
from windows import Ui_MainWindow

matplotlib.use("Qt5Agg")  # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt

matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文
matplotlib.rcParams['axes.unicode_minus'] = False  # 显示负号

def create_sin(self):
    # 生成正弦波
    # 设置采样率和持续时间
    sampling_rate = self.sampling_rate_edit_value  # 采样率 (每秒采样点数)
    duration = self.duration_edit_value  # 持续时间 (秒)

    # 设置正弦波的频率和振幅
    frequency = self.freq_edit_value  # 频率 (赫兹)
    amplitude = self.amp_edit_value  # 振幅

    # 生成时间序列
    t = np.linspace(0, duration, int(sampling_rate * duration), endpoint=False)

    # 生成正弦波采样数据
    samples = amplitude * np.sin(2 * np.pi * frequency * t)
    if self.is_mix:
        # 使用混合频率
        samples_mix = amplitude * np.sin(2 * np.pi * self.mix_freq_edit_value * t)
        # 合并信号
        samples = np.concatenate((samples, samples_mix))
        # samples = samples + samples_mix

    return t, samples

3、FFT傅里叶变换

示例代码:

python 复制代码
X = np.fft.fft(samples)  # 计算信号的FFT
freq = np.fft.fftfreq(len(samples), 1 / self.sampling_rate_edit_value)  # 计算对应的频率数组
mid = len(X) // 2  # 取正值
self.fft_p_show.setData(freq[:mid], np.abs(X)[:mid])  # 绘制FFT

4、STFT 短信傅里叶变换

python 复制代码
# 计算并绘制STFT的大小
fs = self.sampling_rate_edit_value
f, t, spectrum  = signal.stft(data, fs, nperseg=256, noverlap=128)

self.stft_canvas.figure.clear()  # 清空画布
ax = self.stft_fig.add_subplot(111)
self.stft_fig.subplots_adjust(left=None, bottom=0.2, right=None, top=None, wspace=None, hspace=None)
ax.cla()  # 删除原图,让画布上只有新的一次的图
ax.pcolormesh(t, f, np.abs(spectrum), vmin=0, vmax=0.1, shading='gouraud')
ax.set_title('STFT Magnitude')
ax.set_xlabel('time [sec]')
ax.set_ylabel('frequency [Hz]')
self.stft_canvas.draw()
相关推荐
金銀銅鐵19 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
xcyxiner20 小时前
DicomViewer (后台线程处理文件)4
qt
cup111 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
xcyxiner1 天前
DicomViewer (添加模型类)3
qt
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent