1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
|
/**********************************************************************
Audacity: A Digital Audio Editor
FreqWindow.cpp
Dominic Mazzoni
**********************************************************************/
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/brush.h>
#include <wx/image.h>
#include <wx/dcmemory.h>
#include <wx/msgdlg.h>
#include <wx/file.h>
#include <wx/filedlg.h>
#endif
#include <wx/textfile.h>
#include <math.h>
#include "AColor.h"
#include "FreqWindow.h"
#include "FFT.h"
enum {
FirstID = 7000,
FreqCloseButtonID,
FreqExportButtonID,
FreqAlgChoiceID,
FreqSizeChoiceID,
FreqFuncChoiceID,
FreqAxisChoiceID
};
FreqWindow *gFreqWindow = NULL;
#define FREQ_WINDOW_WIDTH 480
#define FREQ_WINDOW_HEIGHT 330
void InitFreqWindow(wxWindow * parent)
{
wxPoint where;
where.x = 150;
where.y = 150;
gFreqWindow = new FreqWindow(parent, -1, "Frequency Analysis", where);
}
// FreqWindow
BEGIN_EVENT_TABLE(FreqWindow, wxFrame)
EVT_CLOSE(FreqWindow::OnCloseWindow)
EVT_SIZE(FreqWindow::OnSize)
EVT_PAINT(FreqWindow::OnPaint)
EVT_BUTTON(FreqCloseButtonID, FreqWindow::OnCloseWindow)
EVT_BUTTON(FreqExportButtonID, FreqWindow::OnExport)
EVT_CHOICE(FreqAlgChoiceID, FreqWindow::OnAlgChoice)
EVT_CHOICE(FreqSizeChoiceID, FreqWindow::OnSizeChoice)
EVT_CHOICE(FreqFuncChoiceID, FreqWindow::OnFuncChoice)
EVT_CHOICE(FreqAxisChoiceID, FreqWindow::OnAxisChoice)
END_EVENT_TABLE()
FreqWindow::FreqWindow(wxWindow * parent, wxWindowID id,
const wxString & title,
const wxPoint & pos):wxFrame(parent, id, title,
pos,
wxSize
(FREQ_WINDOW_WIDTH,
FREQ_WINDOW_HEIGHT)),
mData(NULL), mProcessed(NULL), mBitmap(NULL)
{
mMouseX = 0;
mMouseY = 0;
mArrowCursor = new wxCursor(wxCURSOR_ARROW);
mCrossCursor = new wxCursor(wxCURSOR_CROSS);
mFreqPlot = new FreqPlot(this, 0,
wxPoint(0, 0), wxSize(FREQ_WINDOW_WIDTH, 250));
mUpdateRect.x = 0;
mUpdateRect.y = 0;
mUpdateRect.width = FREQ_WINDOW_WIDTH;
mUpdateRect.height = 250;
mPlotRect.x = 10;
mPlotRect.y = 10;
mPlotRect.width = 460;
mPlotRect.height = 215;
mLeftMargin = 40;
mBottomMargin = 20;
mInfoRect.x = 10;
mInfoRect.y = 230;
mInfoRect.width = 460;
mInfoRect.height = 15;
mExportButton = new wxButton(this, FreqExportButtonID,
"Export...",
wxPoint(390, 260), wxSize(70, 20));
mCloseButton = new wxButton(this, FreqCloseButtonID,
"Close", wxPoint(390, 290), wxSize(70, 20));
#ifndef TARGET_CARBON
mCloseButton->SetDefault();
mCloseButton->SetFocus();
#endif
wxString algChoiceStrings[5] = { "Spectrum",
"Standard Autocorrelation",
"Cuberoot Autocorrelation",
"Enhanced Autocorrelation",
"Cepstrum"
};
mAlgChoice = new wxChoice(this, FreqAlgChoiceID,
wxPoint(10, 260),
wxSize(200, 20), 4, algChoiceStrings);
mAlgChoice->SetSelection(0);
wxString sizeChoiceStrings[8] = { "128",
"256",
"512",
"1024",
"2048",
"4096",
"8192",
"16384"
};
mSizeChoice = new wxChoice(this, FreqSizeChoiceID,
wxPoint(220, 260),
wxSize(160, 20), 8, sizeChoiceStrings);
mSizeChoice->SetSelection(2);
int f = NumWindowFuncs();
wxString *funcChoiceStrings = new wxString[f];
for (int i = 0; i < f; i++)
funcChoiceStrings[i] = WindowFuncName(i) + wxString(" window");
mFuncChoice = new wxChoice(this, FreqFuncChoiceID,
wxPoint(10, 290),
wxSize(200, 20), f, funcChoiceStrings);
mFuncChoice->SetSelection(3);
delete[]funcChoiceStrings;
wxString axisChoiceStrings[2] = { "Linear frequency",
"Log frequency"
};
mAxisChoice = new wxChoice(this, FreqAxisChoiceID,
wxPoint(220, 290),
wxSize(160, 20), 2, axisChoiceStrings);
mAxisChoice->SetSelection(0);
mLogAxis = false;
mBackgroundBrush.SetColour(wxColour(204, 204, 204));
mBackgroundPen.SetColour(wxColour(204, 204, 204));
SetBackgroundColour(wxColour(204, 204, 204));
// Min size, max size
SetSizeHints(FREQ_WINDOW_WIDTH, FREQ_WINDOW_HEIGHT, 20000, 20000);
}
FreqWindow::~FreqWindow()
{
delete mFreqPlot;
if (mBitmap)
delete mBitmap;
delete mCloseButton;
delete mAlgChoice;
delete mSizeChoice;
delete mFuncChoice;
delete mArrowCursor;
delete mCrossCursor;
if (mData)
delete[]mData;
}
void FreqWindow::OnSize(wxSizeEvent & event)
{
int width, height;
GetClientSize(&width, &height);
// Original size was 440 x 330
mUpdateRect.x = 0;
mUpdateRect.y = 0;
mUpdateRect.width = width;
mUpdateRect.height = height - 80;
mFreqPlot->SetSize(0, 0, width, height - 80);
mPlotRect.x = 10;
mPlotRect.y = 10;
mPlotRect.width = width - 20;
mPlotRect.height = height - 115;
mLeftMargin = 40;
mBottomMargin = 20;
mInfoRect.x = 10;
mInfoRect.y = height - 100;
mInfoRect.width = width - 20;
mInfoRect.height = 15;
mExportButton->SetSize(width - 90, height - 70, 70, 20);
mCloseButton->SetSize(width - 90, height - 40, 70, 20);
mAlgChoice->SetSize(10, height - 70, 160, 20);
mSizeChoice->SetSize(180, height - 70, 160, 20);
mFuncChoice->SetSize(10, height - 40, 160, 20);
mAxisChoice->SetSize(180, height - 40, 160, 20);
if (mBitmap)
delete mBitmap;
mBitmap = NULL;
#ifdef __WXMAC__
Refresh(true);
#endif
}
void FreqWindow::PlotMouseEvent(wxMouseEvent & event)
{
if (event.Moving() && event.m_x != mMouseX) {
mMouseX = event.m_x;
mMouseY = event.m_y;
wxRect r = mPlotRect;
r.x += mLeftMargin;
r.width -= mLeftMargin;
r.height -= mBottomMargin;
if (r.Inside(mMouseX, mMouseY))
mFreqPlot->SetCursor(*mCrossCursor);
else
mFreqPlot->SetCursor(*mArrowCursor);
mFreqPlot->Refresh(false);
}
}
void FreqWindow::OnAlgChoice(wxCommandEvent & event)
{
Recalc();
}
void FreqWindow::OnSizeChoice(wxCommandEvent & event)
{
Recalc();
}
void FreqWindow::OnFuncChoice(wxCommandEvent & event)
{
Recalc();
}
void FreqWindow::OnAxisChoice(wxCommandEvent & event)
{
mLogAxis = mAxisChoice->GetSelection();
mFreqPlot->Refresh(false);
}
// If f(0)=y0, f(1)=y1, f(2)=y2, and f(3)=y3, this function finds
// the degree-three polynomial which best fits these points and
// returns the value of this polynomial at a value x. Usually
// 0 < x < 3
float CubicInterpolate(float y0, float y1, float y2, float y3, float x)
{
float a, b, c, d;
a = y0 / -6.0 + y1 / 2.0 - y2 / 2.0 + y3 / 6.0;
b = y0 - 5.0 * y1 / 2.0 + 2.0 * y2 - y3 / 2.0;
c = -11.0 * y0 / 6.0 + 3.0 * y1 - 3.0 * y2 / 2.0 + y3 / 3.0;
d = y0;
float xx = x * x;
float xxx = xx * x;
return (a * xxx + b * xx + c * x + d);
}
float CubicMaximize(float y0, float y1, float y2, float y3)
{
// Find coefficients of cubic
float a, b, c, d;
a = y0 / -6.0 + y1 / 2.0 - y2 / 2.0 + y3 / 6.0;
b = y0 - 5.0 * y1 / 2.0 + 2.0 * y2 - y3 / 2.0;
c = -11.0 * y0 / 6.0 + 3.0 * y1 - 3.0 * y2 / 2.0 + y3 / 3.0;
d = y0;
// Take derivative
float da, db, dc;
da = 3 * a;
db = 2 * b;
dc = c;
// Find zeroes of derivative using quadratic equation
float discriminant = db * db - 4 * da * dc;
if (discriminant < 0.0)
return -1.0; // error
float x1 = (-db + sqrt(discriminant)) / (2 * da);
float x2 = (-db - sqrt(discriminant)) / (2 * da);
// The one which corresponds to a local _maximum_ in the
// cubic is the one we want - the one with a negative
// second derivative
float dda = 2 * da;
float ddb = db;
if (dda * x1 + ddb < 0)
return x1;
else
return x2;
}
float Freq2Pitch(float freq)
{
return float (69.0 + 12.0 * (log(freq / 440.0) / log(2.0)));
}
char gPitchName[10];
char *PitchName(int pitch, bool flats)
{
char *p = gPitchName;
switch (pitch % 12) {
case 0:
*p++ = 'C';
break;
case 1:
if (flats) {
*p++ = 'D';
*p++ = 'b';
} else {
*p++ = 'C';
*p++ = '#';
}
break;
case 2:
*p++ = 'D';
break;
case 3:
if (flats) {
*p++ = 'E';
*p++ = 'b';
} else {
*p++ = 'D';
*p++ = '#';
}
break;
case 4:
*p++ = 'E';
break;
case 5:
*p++ = 'F';
break;
case 6:
if (flats) {
*p++ = 'G';
*p++ = 'b';
} else {
*p++ = 'F';
*p++ = '#';
}
break;
case 7:
*p++ = 'G';
break;
case 8:
if (flats) {
*p++ = 'A';
*p++ = 'b';
} else {
*p++ = 'G';
*p++ = '#';
}
break;
case 9:
*p++ = 'A';
break;
case 10:
if (flats) {
*p++ = 'B';
*p++ = 'b';
} else {
*p++ = 'A';
*p++ = '#';
}
break;
case 11:
*p++ = 'B';
break;
}
sprintf(p, "%d", ((pitch + 3) / 12) - 2);
return gPitchName;
}
float FreqWindow::GetProcessedValue(float freq0, float freq1)
{
int alg = mAlgChoice->GetSelection();
float bin0, bin1, binwidth;
if (alg == 0) {
bin0 = freq0 * mWindowSize / mRate;
bin1 = freq1 * mWindowSize / mRate;
} else {
bin0 = freq0 * mRate;
bin1 = freq1 * mRate;
}
binwidth = bin1 - bin0;
float value = 0.0;
if (binwidth < 1.0) {
float binmid = (bin0 + bin1) / 2.0;
int ibin = int (binmid) - 1;
if (ibin < 1)
ibin = 1;
if (ibin >= mProcessedSize - 3)
ibin = mProcessedSize - 4;
value = CubicInterpolate(mProcessed[ibin],
mProcessed[ibin + 1],
mProcessed[ibin + 2],
mProcessed[ibin + 3], binmid - ibin);
} else {
if (int (bin1) > int (bin0))
value += mProcessed[int (bin0)] * (int (bin0) + 1 - bin0);
bin0 = 1 + int (bin0);
while (bin0 < int (bin1)) {
value += mProcessed[int (bin0)];
bin0 += 1.0;
}
value += mProcessed[int (bin1)] * (bin1 - int (bin1));
value /= binwidth;
}
return value;
}
void FreqWindow::OnPaint(wxPaintEvent & evt)
{
int width, height;
GetSize(&width, &height);
wxPaintDC dc(this);
dc.SetBrush(mBackgroundBrush);
dc.SetPen(mBackgroundPen);
dc.DrawRectangle(0, mUpdateRect.height,
width, height - mUpdateRect.height);
}
void FreqWindow::PlotPaint(wxPaintEvent & evt)
{
wxPaintDC dc(mFreqPlot);
if (!mBitmap)
mBitmap = new wxBitmap(mUpdateRect.width, mUpdateRect.height);
wxMemoryDC memDC;
memDC.SelectObject(*mBitmap);
memDC.SetBrush(mBackgroundBrush);
memDC.SetPen(mBackgroundPen);
memDC.DrawRectangle(0, 0, mUpdateRect.width, mUpdateRect.height);
wxRect r = mPlotRect;
r.x += mLeftMargin;
r.width -= mLeftMargin;
r.height -= mBottomMargin;
memDC.SetPen(*wxBLACK_PEN);
memDC.SetBrush(*wxWHITE_BRUSH);
memDC.DrawRectangle(r);
if (!mProcessed) {
if (mData && mDataLen < mWindowSize)
memDC.DrawText("Not enough data selected.", r.x + 5, r.y + 5);
return;
}
float yTotal = (mYMax - mYMin);
int alg = mAlgChoice->GetSelection();
int i;
#ifdef __WXMSW__
const int fontSize = 8;
#else
const int fontSize = 10;
#endif
wxFont freqWindowFont(fontSize, wxSWISS, wxNORMAL, wxNORMAL);
memDC.SetFont(freqWindowFont);
// Draw y axis and gridlines
if (alg == 0) {
for (float yi = mYMin; yi <= mYMax; yi += mYStep) {
i = (int) yi;
int y =
(int) (r.y + r.height - 1 -
(yi - mYMin) * (r.height - 1) / yTotal);
// Light blue gridline
memDC.SetPen(wxPen(wxColour(204, 204, 255), 1, wxSOLID));
memDC.DrawLine(r.x, y, r.x + r.width, y);
// Pure blue axis line
memDC.SetPen(wxPen(wxColour(0, 0, 255), 1, wxSOLID));
memDC.DrawLine(mInfoRect.x, y, r.x, y);
if (i != (int) mYMin) {
wxString label = wxString::Format("%d dB", i);
long labelWidth, labelHeight;
memDC.GetTextExtent(label, &labelWidth, &labelHeight);
memDC.DrawText(label, r.x - labelWidth - 2, y + 1);
}
}
} else {
wxString label;
long labelWidth, labelHeight;
if (fabs(mYMax) < 1.0)
label.Printf("%.3f", mYMax);
else
label.Printf("%.1f", mYMax);
memDC.GetTextExtent(label, &labelWidth, &labelHeight);
memDC.DrawText(label, r.x - labelWidth - 2, r.y + 2);
if (fabs(mYMin) < 1.0)
label.Printf("%.3f", mYMin);
else
label.Printf("%.1f", mYMin);
memDC.GetTextExtent(label, &labelWidth, &labelHeight);
memDC.DrawText(label,
r.x - labelWidth - 2,
r.y + r.height - labelHeight - 2);
if (mYMax > 0.0 && mYMin < 0.0) {
int y = int ((mYMax / (mYMax - mYMin)) * r.height);
if (y > labelHeight + 4 && y < r.height - 2 * labelHeight - 4) {
label = "0.0";
memDC.GetTextExtent(label, &labelWidth, &labelHeight);
memDC.DrawText(label,
r.x - labelWidth - 2, r.y + y - labelHeight);
// Light blue gridline
memDC.SetPen(wxPen(wxColour(204, 204, 255), 1, wxSOLID));
memDC.DrawLine(r.x, r.y + y, r.x + r.width, r.y + y);
// Pure blue axis line
memDC.SetPen(wxPen(wxColour(0, 0, 255), 1, wxSOLID));
memDC.DrawLine(mInfoRect.x, r.y + y, r.x, r.y + y);
}
}
}
// Draw x axis and gridlines
int width = r.width - 2;
float xMin, xMax, xPos, xRatio, xLast, xStep;
if (alg == 0) {
xMin = mRate / mWindowSize;
xMax = mRate / 2;
xRatio = xMax / xMin;
xPos = xMin;
xLast = xPos / 2.0;
if (mLogAxis)
xStep = pow(2.0, (log(xRatio) / log(2)) / width);
else
xStep = (xMax - xMin) / width;
} else {
xMin = 0;
xMax = mProcessedSize / mRate;
xPos = xMin;
xLast = xPos / 2.0;
xStep = (xMax - xMin) / width;
}
int nextx = 0;
for (i = 0; i < width; i++) {
if ((mLogAxis && xPos / xLast >= 2.0) ||
(!mLogAxis && (i % 60) == 0)) {
int x = i + 1;
// Light blue gridline
memDC.SetPen(wxPen(wxColour(204, 204, 255), 1, wxSOLID));
memDC.DrawLine(r.x + x, r.y, r.x + x, r.y + r.height);
if (x >= nextx) {
// Pure blue axis line
memDC.SetPen(wxPen(wxColour(0, 0, 255), 1, wxSOLID));
memDC.DrawLine(r.x + x, r.y + r.height, r.x + x,
r.y + r.height + 15);
// Label
wxString label;
if (alg == 0) {
if (xPos < 950.0)
label = wxString::Format("%dHz", int (xPos + 0.5));
else
label =
wxString::Format("%dKHz",
int ((xPos / 1000.0) + 0.5));
} else
label = wxString::Format("%.4f s", xPos);
long labelWidth, labelHeight;
memDC.GetTextExtent(label, &labelWidth, &labelHeight);
if (x + labelWidth < width)
memDC.DrawText(label, r.x + x + 3, r.y + r.height + 2);
nextx = x + labelWidth + 4;
}
xLast *= 2.0;
}
if (mLogAxis)
xPos *= xStep;
else
xPos += xStep;
}
// Draw the plot
if (alg == 0)
memDC.SetPen(wxPen(wxColour(50, 150, 50), 1, wxSOLID));
else
memDC.SetPen(wxPen(wxColour(200, 50, 150), 1, wxSOLID));
xPos = xMin;
for (i = 0; i < width; i++) {
float y;
if (mLogAxis)
y = GetProcessedValue(xPos, xPos * xStep);
else
y = GetProcessedValue(xPos, xPos + xStep);
float ynorm = (y - mYMin) / yTotal;
int lineheight = int (ynorm * (r.height - 1));
if (lineheight > r.height - 2)
lineheight = r.height - 2;
if (ynorm > 0.0)
memDC.DrawLine(r.x + 1 + i, r.y + r.height - 1 - lineheight,
r.x + 1 + i, r.y + r.height - 1);
if (mLogAxis)
xPos *= xStep;
else
xPos += xStep;
}
// Find the peak nearest the cursor and plot it
float bestpeak = 0.0;
if (r.Inside(mMouseX, mMouseY)) {
if (mLogAxis)
xPos = xMin * pow(xStep, mMouseX - (r.x + 1));
else
xPos = xMin + xStep * (mMouseX - (r.x + 1));
bool up = (mProcessed[1] > mProcessed[0]);
float bestdist = 1000000;
for (int bin = 2; bin < mProcessedSize; bin++) {
bool nowUp = mProcessed[bin] > mProcessed[bin - 1];
if (!nowUp && up) {
// Local maximum. Find actual value by cubic interpolation
int leftbin = bin - 2;
if (leftbin < 1)
leftbin = 1;
float max = leftbin + CubicMaximize(mProcessed[leftbin],
mProcessed[leftbin + 1],
mProcessed[leftbin + 2],
mProcessed[leftbin + 3]);
float thispeak;
if (alg == 0)
thispeak = max * mRate / mWindowSize;
else
thispeak = max / mRate;
if (fabs(thispeak - xPos) < bestdist) {
bestpeak = thispeak;
bestdist = fabs(thispeak - xPos);
if (thispeak > xPos)
break;
}
}
up = nowUp;
}
int px;
if (mLogAxis)
px = int (log(bestpeak / xMin) / log(xStep));
else
px = int ((bestpeak - xMin) * width / (xMax - xMin));
memDC.SetPen(wxPen(wxColour(200, 0, 0), 1, wxSOLID));
memDC.DrawLine(r.x + 1 + px, r.y, r.x + 1 + px, r.y + r.height);
}
// Outline the graph
memDC.SetPen(*wxBLACK_PEN);
memDC.SetBrush(*wxTRANSPARENT_BRUSH);
memDC.DrawRectangle(r);
// Draw the bevel around the info rect
AColor::Dark(&memDC, false);
memDC.DrawLine(mInfoRect.x, mInfoRect.y,
mInfoRect.x + mInfoRect.width, mInfoRect.y);
memDC.DrawLine(mInfoRect.x, mInfoRect.y,
mInfoRect.x, mInfoRect.y + mInfoRect.height);
AColor::Light(&memDC, false);
memDC.DrawLine(mInfoRect.x, mInfoRect.y + mInfoRect.height,
mInfoRect.x + mInfoRect.width,
mInfoRect.y + mInfoRect.height);
memDC.DrawLine(mInfoRect.x + mInfoRect.width, mInfoRect.y,
mInfoRect.x + mInfoRect.width,
mInfoRect.y + mInfoRect.height);
// If the mouse cursor is pointing inside the graph, print out info about the
// cursor location
if (r.Inside(mMouseX, mMouseY)) {
float value;
if (mLogAxis) {
xPos = xMin * pow(xStep, mMouseX - (r.x + 1));
value = GetProcessedValue(xPos, xPos * xStep);
} else {
xPos = xMin + xStep * (mMouseX - (r.x + 1));
value = GetProcessedValue(xPos, xPos + xStep);
}
wxString info;
if (alg == 0) {
wxString xpitch = PitchName(int (Freq2Pitch(xPos) + 0.5), false);
wxString peakpitch =
PitchName(int (Freq2Pitch(bestpeak) + 0.5), false);
const char *xp = (const char *) xpitch;
const char *pp = (const char *) peakpitch;
info.Printf("Cursor: %d Hz (%s) = %d dB "
"Peak: %d Hz (%s)",
int (xPos + 0.5),
xp, int (value + 0.5), int (bestpeak + 0.5), pp);
} else if (xPos > 0.0 && bestpeak > 0.0) {
wxString xpitch =
PitchName(int (Freq2Pitch(1.0 / xPos) + 0.5), false);
wxString peakpitch =
PitchName(int (Freq2Pitch(1.0 / bestpeak) + 0.5), false);
const char *xp = (const char *) xpitch;
const char *pp = (const char *) peakpitch;
info.Printf("Cursor: %.4f sec (%d Hz) (%s) = %f, "
"Peak: %.4f sec (%d Hz) (%s)",
xPos,
int (1.0 / xPos + 0.5),
xp, value, bestpeak, int (1.0 / bestpeak + 0.5), pp);
}
memDC.DrawText(info, mInfoRect.x + 2, mInfoRect.y + 2);
}
dc.Blit(0, 0, mUpdateRect.width, mUpdateRect.height,
&memDC, 0, 0, wxCOPY, FALSE);
}
void FreqWindow::OnCloseWindow(wxCloseEvent & WXUNUSED(event))
{
this->Show(FALSE);
}
void FreqWindow::Plot(int len, float *data, double rate)
{
mRate = rate;
mDataLen = len;
if (mData)
delete[]mData;
mData = new float[len];
for (int i = 0; i < len; i++)
mData[i] = data[i];
Recalc();
}
void FreqWindow::Recalc()
{
if (mProcessed)
delete mProcessed;
mProcessed = NULL;
if (!mData) {
mFreqPlot->Refresh(false);
return;
}
int alg = mAlgChoice->GetSelection();
int windowFunc = mFuncChoice->GetSelection();
long windowSize = 0;
(mSizeChoice->GetStringSelection()).ToLong(&windowSize);
if (!(windowSize >= 32 && windowSize <= 65536 &&
alg >= 0 && alg <= 3 && windowFunc >= 0 && windowFunc <= 3)) {
mFreqPlot->Refresh(false);
return;
}
mWindowSize = windowSize;
if (mDataLen < mWindowSize) {
mFreqPlot->Refresh(false);
return;
}
mProcessed = new float[mWindowSize];
int i;
for (i = 0; i < mWindowSize; i++)
mProcessed[i] = 0.0;
int half = mWindowSize / 2;
float *in = new float[mWindowSize];
float *in2 = new float[mWindowSize];
float *out = new float[mWindowSize];
float *out2 = new float[mWindowSize];
int start = 0;
int windows = 0;
while (start + mWindowSize <= mDataLen) {
for (i = 0; i < mWindowSize; i++)
in[i] = mData[start + i];
WindowFunc(windowFunc, mWindowSize, in);
switch (alg) {
case 0: // Spectrum
PowerSpectrum(mWindowSize, in, out);
for (i = 0; i < half; i++)
mProcessed[i] += out[i];
break;
case 1:
case 2:
case 3: // Autocorrelation, Cuberoot AC or Enhanced AC
// Take FFT
FFT(mWindowSize, false, in, NULL, out, out2);
// Compute power
for (i = 0; i < mWindowSize; i++)
in[i] = (out[i] * out[i]) + (out2[i] * out2[i]);
if (alg == 1) {
for (i = 0; i < mWindowSize; i++)
in[i] = sqrt(in[i]);
}
if (alg == 2 || alg == 3) {
// Tolonen and Karjalainen recommend taking the cube root
// of the power, instead of the square root
for (i = 0; i < mWindowSize; i++)
in[i] = pow(in[i], 1.0 / 3.0);
}
// Take FFT
FFT(mWindowSize, false, in, NULL, out, out2);
// Take real part of result
for (i = 0; i < half; i++)
mProcessed[i] += out[i];
break;
case 4: // Cepstrum
FFT(mWindowSize, false, in, NULL, out, out2);
// Compute log power
for (i = 0; i < mWindowSize; i++)
in[i] = log((out[i] * out[i]) + (out2[i] * out2[i]));
// Take IFFT
FFT(mWindowSize, true, in, NULL, out, out2);
// Take real part of result
for (i = 0; i < half; i++)
mProcessed[i] += out[i];
break;
} //switch
start += half;
windows++;
}
switch (alg) {
case 0: // Spectrum
// Convert to decibels
for (i = 0; i < half; i++)
mProcessed[i] = 10 * log10(mProcessed[i] / mWindowSize / windows);
mProcessedSize = half;
mYMin = -90;
mYMax = 10;
mYStep = 10;
break;
case 1: // Standard Autocorrelation
case 2: // Cuberoot Autocorrelation
for (i = 0; i < half; i++)
mProcessed[i] = mProcessed[i] / windows;
// Find min/max
mYMin = mProcessed[0];
mYMax = mProcessed[0];
for (i = 1; i < half; i++)
if (mProcessed[i] > mYMax)
mYMax = mProcessed[i];
else if (mProcessed[i] < mYMin)
mYMin = mProcessed[i];
mYStep = 1;
mProcessedSize = half;
break;
case 3: // Enhanced Autocorrelation
for (i = 0; i < half; i++)
mProcessed[i] = mProcessed[i] / windows;
// Peak Pruning as described by Tolonen and Karjalainen, 2000
// Clip at zero, copy to temp array
for (i = 0; i < half; i++) {
if (mProcessed[i] < 0.0)
mProcessed[i] = 0.0;
out[i] = mProcessed[i];
}
// Subtract a time-doubled signal (linearly interp.) from the original
// (clipped) signal
for (i = 0; i < half; i++)
if ((i % 2) == 0)
mProcessed[i] -= out[i / 2];
else
mProcessed[i] -= ((out[i / 2] + out[i / 2 + 1]) / 2);
// Clip at zero again
for (i = 0; i < half; i++)
if (mProcessed[i] < 0.0)
mProcessed[i] = 0.0;
// Find new min/max
mYMin = mProcessed[0];
mYMax = mProcessed[0];
for (i = 1; i < half; i++)
if (mProcessed[i] > mYMax)
mYMax = mProcessed[i];
else if (mProcessed[i] < mYMin)
mYMin = mProcessed[i];
mYStep = 1;
mProcessedSize = half;
break;
case 4: // Cepstrum
for (i = 0; i < half; i++)
mProcessed[i] = mProcessed[i] / windows;
// Find min/max, ignoring first and last few values
int ignore = 4;
mYMin = mProcessed[ignore];
mYMax = mProcessed[ignore];
for (i = ignore + 1; i < half - ignore; i++)
if (mProcessed[i] > mYMax)
mYMax = mProcessed[i];
else if (mProcessed[i] < mYMin)
mYMin = mProcessed[i];
mYStep = 1;
mProcessedSize = half;
break;
}
delete[]in;
delete[]in2;
delete[]out;
delete[]out2;
mFreqPlot->Refresh(false);
}
void FreqWindow::OnExport()
{
wxString fName = "spectrum.txt";
fName = wxFileSelector("Export Spectral Data As:",
NULL, fName, "txt", "*.txt", wxSAVE, this);
if (fName == "")
return;
wxTextFile f(fName);
f.Create();
f.Open();
if (!f.IsOpened()) {
wxMessageBox("Couldn't write to " + fName);
return;
}
if (mAlgChoice->GetSelection() == 0) {
f.AddLine("Frequency (Hz)\tLevel (dB)");
for (int i = 1; i < mProcessedSize; i++)
f.AddLine(wxString::
Format("%f\t%f", i * mRate / mWindowSize,
mProcessed[i]));
} else {
f.AddLine("Lag (seconds)\tFrequency (Hz)\tLevel");
for (int i = 1; i < mProcessedSize; i++)
f.AddLine(wxString::Format("%f\t%f\t%f",
i / mRate, 1.0 / i, mProcessed[i]));
}
#ifdef __WXMAC__
f.Write(wxTextFileType_Mac);
#else
f.Write();
#endif
f.Close();
}
BEGIN_EVENT_TABLE(FreqPlot, wxWindow)
EVT_PAINT(FreqPlot::OnPaint)
EVT_MOUSE_EVENTS(FreqPlot::OnMouseEvent)
END_EVENT_TABLE()
FreqPlot::FreqPlot(wxWindow * parent, wxWindowID id,
const wxPoint & pos,
const wxSize & size):wxWindow(parent, id, pos, size)
{
freqWindow = (FreqWindow *) parent;
}
void FreqPlot::OnPaint(wxPaintEvent & evt)
{
freqWindow->PlotPaint(evt);
}
void FreqPlot::OnMouseEvent(wxMouseEvent & event)
{
freqWindow->PlotMouseEvent(event);
}
|