| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1467 人关注过本帖
标题:正弦曲线波峰波谷计数不对
只看楼主 加入收藏
lvguidong123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2012-10-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
正弦曲线波峰波谷计数不对
我目前在做类似正弦曲线的趋势图,我想记录每一个波峰或者波谷的个数,具体实现是每经过波峰加1,每经过波谷再加1,可是它却每次只在波谷的时候加2,波峰的时候不加任何数。k显示的是0,2,4,6……,怎么回事啊?请大家帮忙,这是我的代码,比较简单,很容易理解。
If (py(t - 1) < py(t) And py(t - 2) > py(t - 1)) Or (py(t - 1) > py(t) And py(t - 2) < py(t - 1)) Then
k = k + 1
End If
搜索更多相关主题的帖子: 记录 
2012-12-04 16:54
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:20 
最好把你的 py 这个函数也贴出来,才能测试。才能知道问题出在哪。

授人于鱼,不如授人于渔
早已停用QQ了
2012-12-04 17:14
lvguidong123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2012-10-24
收藏
得分:0 
回复 2楼 风吹过b
Dim px(5000) As Single, py(5000) As Single
Dim ab(5) As Byte
Public DataFromCom As Integer
Public DataFromComLast As Integer '上次的串口值
Public Data As Single
Public TimeCount As Integer
Public Buffer As Variant
Public w As Integer
Public k As Integer
Public j As Integer
Private Sub Command2_Click()
Form2.Show vbModal
End Sub
Private Sub Form_activate() '定义坐标系`
Picture1.Scale (-30, 10500)-(760, -1000)
Picture1.Line (750, 0)-(-30, 0), vbBlack
Picture1.Line (0, 10000)-(0, -1000)
Dim i As Integer
'标x轴刻度
For i = 0 To 750 Step 30
Picture1.Line (i, 10000)-(i, 0)
Picture1.CurrentX = i - 16: Picture1.CurrentY = -20: Picture1.Print i
Next i
'标y轴刻度
For i = 0 To 10000 Step 1000
Picture1.Line (750, i)-(0, i)
Next i
End Sub
Private Sub Command1_Click()
With MSComm1
If Command1.Caption = "打开" Then '判断通信口是否打开
MSComm1.PortOpen = True '打开通信口
Timer1.Enabled = True
Command1.Caption = "关闭"
FileName = Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "-" & Hour(Time) & "-" & Minute(Time) & ".txt"
Open "c:\采集数据\" & FileName For Append As #1
Else
MSComm1.PortOpen = False
Command1.Caption = "打开"
Timer1.Enabled = False
Close #1
TimeCount = 0
If Err Then '错误处理
MsgBox "串口通信无效"
Text1.Text = ""
End If
End If
End With
End Sub
Private Sub MSComm1_OnComm()
With MSComm1
Select Case
Case comEvReceive '是接收事件
Buffer = MSComm1.Input '读取一个字节
ab(1) = Buffer(0) '转换字节数据类型数组
If ab(1) = &HFF Then '判断是否为数据开始标志
MSComm1.RThreshold = 0 '关闭OnComm 事件接收
Do
DoEvents
Loop Until MSComm1.InBufferCount >= 4
Buffer = MSComm1.Input
ab(2) = Buffer(0) - 48
Buffer = MSComm1.Input
ab(3) = Buffer(0) - 48
Buffer = MSComm1.Input
ab(4) = Buffer(0) - 48
Buffer = MSComm1.Input
ab(5) = Buffer(0) - 48
b1 = CInt(ab(2))
b2 = CInt(ab(3))
b3 = CInt(ab(4))
b4 = CInt(ab(5))
Data = 20 * (b1 + b2 / 10 + b3 / 100 + b4 / 1000)
DataFromCom = 100 * Data
MSComm1.RThreshold = 1 '打开OnComm 事件接收
End If
Text1.Text = Data
Case Else
End Select
End With
End Sub

Private Sub Timer1_Timer()
Picture1.Cls
DataFromComLast = DataFromCom
Randomize
Picture1.ScaleMode = 0
Picture1.Scale (-30, 10500)-(760, -1000)
Picture1.Line (750, 0)-(-30, 0), vbBlack
Picture1.Line (0, 10000)-(0, -1000)
Dim i As Integer
'标x轴刻度
For i = 0 To 750 Step 30
Picture1.Line (i, 10000)-(i, 0)
Picture1.CurrentX = i - 16: Picture1.CurrentY = -200: Picture1.Print i
Next i
'标y轴刻度
For i = 0 To 10000 Step 1000
Picture1.Line (750, i)-(0, i)
Next i
If TimeCount >= 0 And TimeCount <= 750 Then
For u = TimeCount To TimeCount
x = u
px(u) = x '给各数据点横坐标赋值
py(u) = DataFromCom  '给各数据点纵坐标赋值
Next u
Else
For t = 1 To 749
py(t) = py(t + 1)
px(t) = t
Next t
py(750) = DataFromCom
End If
k = 1
For t = 2 To 749
If (py(t - 1) < py(t) And py(t - 2) > py(t - 1)) Or (py(t - 1) > py(t) And py(t - 2) < py(t - 1)) Then
k = k + 1
End If
Next t
TimeCount = TimeCount + 1
DrawRealLine Picture1, TimeCount, DataFromCom, DataFromComLast '画出实时的曲线
Print #1, TimeCount / 2, Data
Text2.Text = k
End Sub
Private Sub DrawRealLine(picX As PictureBox, TimeCountX As Integer, DataFromComX As Integer, DataFromComLastX As Integer)
If TimeCountX - 1 > 0 And TimeCountX - 1 < 749 Then
For i = 1 To TimeCountX
picX.Line -(px(i - 1), py(i - 1)), vbRed
Next i
Else
For i = 1 To 750
picX.Line -(px(i - 1), py(i - 1)), vbRed
Next i
End If
End Sub
2012-12-05 10:13
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
从下位机取得的数据,头大。无法测试。

那只能你自己 debug 了。
你这样,每次画点时,都随手把 py 的值显示在 点的附近,然后再看一下,这个pY的值是不是 与测试条件矛盾。

如果你不想每次调用时都连接下位机的话,那你只能把 500 个点的数据,保存一份,然后模拟测试。

授人于鱼,不如授人于渔
早已停用QQ了
2012-12-05 11:18
快速回复:正弦曲线波峰波谷计数不对
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017257 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved